用struct初始化C 向量时的构造函数误差

Constructor error when initializing c++ vector with struct

本文关键字:构造函数 误差 向量 struct 初始化      更新时间:2023-10-16

我试图初始化一个 opcodeTable的向量,其中2个值如下:

struct opcodeTableE {
        uint16_t opcode;
        uint16_t mask;
        void (chipCpu::*instruction)(uint16_t);
};
std::vector<opcodeTableE> opcodetable{
        {0x00E0, 0xFFFF, chipCpu::clearScreen},
        {0x00EE, 0xFFFF, chipCpu::returnFromSub}
};

但是我会收到以下错误:

no instance of constructor "std::vector<_Tp, _Alloc>::vector [with _Tp=chipCpu::opcodeTableE, _Alloc=std::allocator<chipCpu::opcodeTableE>]" matches the argument list -- argument types are: ({...}, {...})

注意:我在C 14

您需要使用 operator&获取成员函数的指针。例如

std::vector<opcodeTableE> opcodetable{
        {0x00E0, 0xFFFF, &chipCpu::clearScreen},
        {0x00EE, 0xFFFF, &chipCpu::returnFromSub}
};

live

btw:operator&仅在获取非会员函数的指针或静态成员函数时才是可选的。