不能用作成员指针,因为它是 'void (*)()' 类型

cannot be used as a member pointer, since it is of type 'void (*)()'

我试图取消引用存储在静态数组中的方法指针并从方法中调用它,但出现以下错误:

error: 'chip8::Chip8::table[0]' cannot be used as a member pointer, since it is of type 'void (*)()'
     (this->*table[0])();
                    ^

这是我的 class 声明 (chip.hpp):

class Chip8{
  private:
    static void (*table[16])(); //function pointer table
    //...
  public:
    void cycle();
    //...
};

这里是 Chip8::cycle 和 Chip8::table (chip.cpp) 的实现:

void (Chip8::*table[16])() = {
    &Chip8::opcode0,
    &Chip8::JP_1nnn,
    &Chip8::CALL_2nnn,
    &Chip8::SE_3xkk,
    &Chip8::SNE_4xkk,
    &Chip8::SE_5xy0,
    &Chip8::LD_6xkk,
    &Chip8::ADD_7xkk,
    &Chip8::opcode8,
    &Chip8::SNE_9xy0,
    &Chip8::LD_Annn,
    &Chip8::JP_Bnnn,
    &Chip8::RND_Cxkk,
    &Chip8::DRW_Dxyn,
    &Chip8::opcodeE,
    &Chip8::opcodeF
  };

  void Chip8::cycle(){
    opcode = (memory[pc] << 8) | memory[pc+1];
    pc+=2;

    (this->*table[0])(); // error here

    if(dt > 0){
      dt--;
    }

    if(st > 0){
      st--;
    }
  }

编辑: 以下是分配给 table 的函数声明:

//Function table extensions
    void opcode0();
    void opcode8();
    void opcodeE();
    void opcodeF();

    //Instructions
    void CLS_00E0();  // Clears screen
    void RET_00EE();  // Pops new pc off the stack
    void JP_1nnn();   // Jumps to nnn
    void CALL_2nnn(); // Pushes current pc to stack and jumps to nnn
    void SE_3xkk();   // Skip next instruction if Vx = kk
    void SNE_4xkk();  // Skip next instruction if Vx != kk
    void SE_5xy0();   // Skip next instruction if Vx == Vy
    void LD_6xkk();   // Set Vx = kk
    void ADD_7xkk();  // Vx += kk
    void LD_8xy0();   // Vx = Vy
    void OR_8xy1();   // Vx |= Vy
    void AND_8xy2();  // Vx &= Vy
    void XOR_8xy3();  // Vx ^= Vy
    void ADD_8xy4();  // Vx += Vy, if the sum is greater than 255, then VF = 1
    void SUB_8xy5();  // Vx -= Vy, if Vx > Vy, then VF = 1, otherwise VF = 0
    void SHR_8xy6();  // Vx >>= 1, if least significant bit of Vx is 1, then VF is set to 1, otherwise 0
    void SUBN_8xy7(); // Vx = Vy - Vx, if Vy > Vx, VF = 1, otherwise VF = 0
    void SHL_8xyE();  // Vx <<= 1, if most significant bit of Vx is 1, then VF is set to 1, otherwise 0
    void SNE_9xy0();  // Skip next instruction if Vx != Vy
    void LD_Annn();   // I = nnn
    void JP_Bnnn();   // Jumps to nnn + V0
    void RND_Cxkk();  // Vx = random byte & kk
    void DRW_Dxyn();  // Draws n-byte (n = height) sprite starting at Vx and Vy. If collision detected, VF = 1
    void SKP_Ex9E();  // Skip next instruction if value of Vx is pressed
    void SKNP_ExA1(); // Skip next instruction if value of Vx isn't pressed
    void LD_Fx07();   // Set Vx = delay timer
    void LD_Fx0A();   // Wait for key press and store value in Vx
    void LD_Fx15();   // Set delay timer = Vx
    void LD_Fx18();   // Set sound timer = Vx
    void ADD_Fx1E();  // Set I += Vx
    void LD_Fx29();   // Set I = memory location of digit sprite in Vx
    void LD_Fx33();   // Store BCD representation of value of Vx in I, I+1, I+2
    void LD_Fx55();   // Store registers V0 through Vx (including) starting at memory location I
    void LD_Fx65();   // Read registers V0 through Vx (including) starting at memory location I

这是什么问题,我该如何解决?

您的 table 包含函数指针,而不是成员函数指针。只需将它们用作没有 this.

的函数指针

我猜分配给 table 的所有函数都是静态函数。


对于非静态函数,使用成员函数指针类型 table:

static void (Chip::*table[16])(); // member function pointer table

或更具可读性:

using OpType = void (Chip::*)();
static OpType table[16];

问题是当你写的时候:

static void (*table[16])();

您正在声明一个名为 table 静态数据成员,它是一个 大小为 16 的数组,其元素是指向的指针自由函数,没有参数,return 类型为 int

但您真正想要的是一个 table,它是一个 大小为 16 的数组,其元素是指向 成员函数的指针 Chip8,您可以如下所示进行操作:

class Chip8{
  private:
//---------------vvvvvvvv------------>note the added Chip8::* indicating that a pointer to a member function instead of free function            
    static void (Chip8::*table[16])(); //function pointer table
  
  public:
    void cycle();
    void opcode0();
    
   
};
//definition for the static data member `table`
void (Chip8::*Chip8::table[16])() = {
    &Chip8::opcode0,
    
  };
void Chip8::opcode0()
{
    std::cout<<"opcode0 called"<<std::endl;
}
void Chip8::cycle(){
  
    std::cout<<"cycle called"<<std::endl;
    (this->*table[0])();
    
  }

int main()
{
    Chip8 chip;
    chip.cycle();
    return 0;
}

Working Demo.


请注意,您可以使用 别名声明 使上述代码更 可读,如下所示:

class Chip8{
  private:
    using type = void (Chip8::*)();              
    static type table[16]; //function pointer table
  
  public:
    void cycle();
    void opcode0();
    
   
};
Chip8::type Chip8::table[16] = {
    &Chip8::opcode0,
    
  };
void Chip8::opcode0()
{
    std::cout<<"opcode0 called"<<std::endl;
}
void Chip8::cycle(){
  
    std::cout<<"cycle called"<<std::endl;
    (this->*table[0])();
    
}
int main()
{
    Chip8 chip;
    chip.cycle();
    return 0;
}

Demo