0

I've checked multiple answers that are asking similar things but nothing has been working with me so far.

here's my class

class VirtualMachine
{
private: 
    typedef void(VirtualMachine::*FunctionOfOperator)(Operands);
    //a map is used to link operator functions with the enum 
    //representing the operator in the instruction
    //to call the function representing the ADD operator:
    //instructionFunctions[ADD](Operands);
    //this will execute the ADD instructions with the operands 
    //passed to the function

    std::map<Operator, FunctionOfOperator> instructionFunctions;

        //operator functions
        void _ADD(Operands operands),   _SUB(Operands operands),    _MUL(Operands operands),    _DIV(Operands operands), 
            _IDIV(Operands operands),   _IREM(Operands operands),   _ABS(Operands operands),    _RND(Operands operands);

        void mapInstructions();
...

    }

void VirtualMachine::mapInstructions()
{
    instructionFunctions[Operator::ADD]  = &VirtualMachine::_ADD;
    instructionFunctions[Operator::SUB]  = &VirtualMachine::_SUB;
    instructionFunctions[Operator::MUL]  = &VirtualMachine::_MUL;
    instructionFunctions[Operator::DIV]  = &VirtualMachine::_DIV;
    instructionFunctions[Operator::IDIV] = &VirtualMachine::_IDIV;
    instructionFunctions[Operator::IREM] = &VirtualMachine::_IREM;
    instructionFunctions[Operator::ABS]  = &VirtualMachine::_ABS;
    instructionFunctions[Operator::RND]  = &VirtualMachine::_RND;
}

To call the function as shown in this answer:

(this->*instructionFunctions[translated_memory.at(pc).op])(translated_memory.at(pc).operands);

this is called inside VirtualMachine::runVM() which is a public function

I've also tried using std::function with bind as shown here

I am getting error LNK2019: unresolved external symbol

Can someone please explain more about pointer to private memeber function not just how to solve this. before the Virtual Machine was not a class and the map to a pointer function was working fine and I was able to call functions.

thank you.

Community
  • 1
  • 1
T.J.
  • 61
  • 5
  • 1
    LNK2019 means your code compiles fine (may still be riddled with logic errors mind you, but such is life), but when it tries to assemble all of the compiled pieces into a program something is missing. Your reproduction of the error message leaves out what that "something missing" is, making it harder to help you. – user4581301 Oct 25 '16 at 18:08
  • 1
    Note that names of your operator functions lead to UB – Slava Oct 25 '16 at 18:10
  • There is no such thing as pointer to private method, there is just a pointer to method. – Slava Oct 25 '16 at 18:12
  • Does the linker give you the name of the missing symbol? – Alden Oct 25 '16 at 18:17
  • Expanding on @Slava's comment: [What are the rules about using an underscore in a C++ identifier?](http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – user4581301 Oct 25 '16 at 18:42
  • @Alden yes it is the following: `VirtualMachine.obj : error LNK2019: unresolved external symbol "private: void __cdecl VirtualMachine::_ADD(class std::vector >)" (?_ADD@VirtualMachine@@AEAAXV?$vector@UOperand@@V?$allocator@UOperand@@@std@@@std@@@Z) referenced in function "private: void __cdecl VirtualMachine::mapInstructions(void)" (?mapInstructions@VirtualMachine@@AEAAXXZ)` – T.J. Oct 25 '16 at 22:40
  • @user4581301 link error in the top comment. Thank you. – T.J. Oct 25 '16 at 22:41
  • See it and in the provided code I only see a declaration of `_ADD`.Pretty freaky method declarations, by the way. Never seen the comma operator used that way before. Kind of surprised it works. Anyway, linker can't find `_ADD` and I can't either, so I have to question whether `_ADD` was implemented. – user4581301 Oct 25 '16 at 23:28
  • @user4581301 okay now I feel pretty stupid, I didnt have the include header were I implement it. thanks. Is there a good source were I can learn more about the link errors, what everyone mean? – T.J. Oct 25 '16 at 23:38
  • [The horse's mouth](https://msdn.microsoft.com/en-us/library/8x5x43k7.aspx). Recommend against implementing the functions in a header. If you are going to separate them from the class definition, and the class isn't a template, implement them in a cpp file. You'll generally get quicker builds that way. – user4581301 Oct 25 '16 at 23:45
  • @user4581301 thanks for the advice! – T.J. Oct 25 '16 at 23:48

0 Answers0