【问题标题】:Save functions in Arrays C++在数组 C++ 中保存函数
【发布时间】:2016-09-11 16:03:22
【问题描述】:

我有一个新问题,我正在使用一个名为解释器的类:

解释器.h

#ifndef INTERPRETER_H_
#define INTERPRETER_H_

#include <string>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

using namespace std;

class Interpreter {
public:
   static Interpreter* getInstance();
   typedef void (*fn)(int, int, int, int);
   static fn opcodes[44];
   static fn functions[43];
private:
   Interpreter() {
      opcodes[9] = addiu;
      opcodes[3] = jal;
      opcodes[8] = addi;
      opcodes[4] = beq;
      opcodes[43] = sw;

      functions[32] = add;
      functions[33] = addu;
      functions[34] = sub;
      functions[18] = mflo;
      functions[26] = div;
      functions[12] = syscall;
      functions[8] = jr;

}
;
void addiu(int, int, int, int);
void addi(int, int, int, int);
void jal(int, int, int, int);
void beq(int, int, int, int);
void sw(int, int, int, int);

void add(int, int, int, int);
void addu(int, int, int, int);
void sub(int, int, int, int);
void mflo(int, int, int, int);
void div(int, int, int, int);
void syscall(int, int, int, int);
void jr(int, int, int, int);


static Interpreter* _instance;
};

 #endif /* INTERPRETER_H_ */

解释器.cpp

#include "Interpreter.h"

Interpreter* Interpreter::_instance = NULL;

Interpreter* Interpreter::getInstance() {
if (_instance == NULL) {
    _instance = new Interpreter();
}
return _instance;
}



 void Interpreter::addiu(int rs, int rt, int rd, int shamt) {

 }

 void Interpreter::addi(int rs, int rt, int rd, int shamt) {

 }

 void Interpreter::jal(int rs, int rt, int rd, int shamt) {

 }

 void Interpreter::beq(int rs, int rt, int rd, int shamt) {

 }

 void Interpreter::sw(int rs, int rt, int rd, int shamt) {

 }



 void Interpreter::add(int rs, int rt, int rd, int shamt) {

 }

 void Interpreter::addu(int rs, int rt, int rd, int shamt) {

 }

 void Interpreter::sub(int rs, int rt, int rd, int shamt) {

 }

 void Interpreter::mflo(int rs, int rt, int rd, int shamt) {

 }

 void Interpreter::div(int rs, int rt, int rd, int shamt) {

 }

 void Interpreter::syscall(int rs, int rt, int rd, int shamt) {

 }

 void Interpreter::jr(int rs, int rt, int rd, int shamt) {

 }

但是当我尝试将函数保存在数组操作码或函数中时,程序在 .h 文件中存在错误,恰好在私有构造函数 Interpreter() 中。

错误是:

无法将 'Interpreter::addiu' 从类型 'void (Interpreter::)(int, int, int, int)' 转换为类型 'Interpreter::fn {aka void (*)(int, int, int, int)}'

谢谢你的帮助

【问题讨论】:

  • A minimal reproducible example 应该是最小的。请将您发布的代码减少到绝对最低限度。 (回答您的问题:您正在声明函数指针数组,但尝试分配成员函数指针。这些是非常不同的野兽。)
  • 尝试指向成员函数的指针,意思是:typedef void (Interpreter::*fn)(int, int, int, int);
  • 我试过了,但是不行,这是错误:cannot convert ‘Interpreter::addiu’ from type ‘void (Interpreter::)(int, int, int, int)’ to type ‘Interpreter::fn {aka void (Interpreter::*)(int, int, int, int)}’

标签: c++ arrays function pointers save


【解决方案1】:

您不能使用普通函数指针指向非静态成员函数。 您需要使用成员函数指针:

typedef void (Interpreter::*fn)(int, int, int, int);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-05-28
    • 2012-10-25
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    相关资源
    最近更新 更多