1

I am trying to initialize a vector of struct opcodeTable with 2 values as follow:

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}
};

But I get the following error:

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: ({...}, {...})

NOTE: I am on C++14

0

1 Answer 1

4

You need to use operator& to get the pointer to member function. e.g.

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

LIVE

BTW: operator& is only optional when getting pointer to non-member function or static member function because of the function-to-pointer implicit conversion.

Sign up to request clarification or add additional context in comments.

2 Comments

Here is what I got after doing it: error: could not convert '{{224, 65535, &chipCpu::clearScreen}, {238, 65535, &chipCpu::returnFromSub}}' from '<brace-enclosed initializer list>' to 'std::vector<chipCpu::opcodeTableE>'
@KnoxRoot Could you make an mcve which could be used to reproduce the error? I tried to make one here and it works fine.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.