将函数保存在数组C++中
Save functions in Arrays C++
我有一个新问题,我正在使用一个名为解释器的类:
口译员.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::addu"从类型"void(Interpreter::)(int,int,int)"转换为类型"Interpreter::fn{aka void(*)(int、int、int)}"
谢谢你的帮助我
不能用普通函数指针指向非静态成员函数。您需要使用成员函数指针:
typedef void (Interpreter::*fn)(int, int, int, int);
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '