2016-05-15 84 views
0

我有一個新的問題,我使用的是類命名的解釋:保存功能++

Interpreter.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_ */ 

Interpreter.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()中,當我嘗試將函數保存在數組操作碼或函數中時。

錯誤是:

不能轉換 '翻譯:: addiu' 從類型 '無效(口譯::)(INT,INT,INT,INT)' 爲類型「解釋:: FN {又名空隙( *)(INT,INT,INT,INT)}」

感謝您的幫助我

+2

[mcve]應該是最小的。請將您發佈的代碼減少到絕對必要的最低限度。 (回答你的問題:你正在聲明函數指針數組,但是試着分配成員函數指針,它們是**非常不同的野獸。) – IInspectable

+0

嘗試指向成員函數的指針,意思是:'typedef void(Interpreter :: * (int,int,int,int);' – SHR

+0

我嘗試但不工作,這是錯誤: '不能從類型'void(Interpreter ::)(int,int)'轉換'Interpreter :: addiu' ,int,int)'來鍵入'Interpreter :: fn {aka void(Interpreter :: *)(int, int,int,int)}'' –

回答

0

您不能指向一個正常的函數指針非靜態成員函數。 您需要使用成員函數指針:

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