0

I am still getting the hang of polymorphism, and I wanted to see if you all could point out the error I am making, and make sence of the linker error I am getting as a result.

In summary, I have a parent class that has a virtual function that I want to override in the child classes, but when doing the approach below I get a linker error in the object files.

P.S. I am simply trying to reach one of the sub-classes members from the parent class, but it does not have a function to retrieve that sub class member, and if I try to write one the parent class has no idea what or who that subclass member is, so I went about it using the virtual function.

AGAM.h:

//------------- Parent class header file ( also this class is instantiated so not abstract ) ---

virtual std::string getHardwareName(); //original declaration

AGAM.cpp:

//----------- Parent cpp file ----------------------------
std::string getHardwareName()  // parent class definition 
{
    return NULL;
}  

TUI.h:

//-------------- child class header file ---------
std::string getHardwareName(); //child class override 

TUI.cpp:

//--------------- child class cpp file ----------

std::string getHardwareName(){  // override definition
    return mBoardName;
} 

I am using MS Visual Studio, and below is the linking errors I am getting:

Error LNK2001 unresolved external symbol "public: virtual class std::basic_string<char,struct std::char_traits,class std::allocator > __thiscall AGAM::getHardwareName(void)" (?getHardwareName@AnalogGroupAssignmentManager@Boards@TIU@@UAE?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) TOC C:\path\to\object\file.obj) 1

And I get the same error for the child obj files.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
MacLCM
  • 57
  • 9
  • 1
    `std::string getHardwareName()` -> `std::string AGAM::getHardwareName()` – user4581301 Jun 07 '21 at 21:09
  • 1
    You should use the override keyword from C++ to help you. It can point out some of these errors: https://en.cppreference.com/w/cpp/language/override . – zilleplus Jun 07 '21 at 21:52
  • https://godbolt.org/z/EebMaodbW -> worked out the whole thing,,, – zilleplus Jun 07 '21 at 21:58
  • @zilleplus ok now that is a cool tool I have not yet seen. thanks man and thank you for pointing me to the other answer who ever closed the question it did help me resolve the issue and I got it to work more than one way actually. – MacLCM Jun 08 '21 at 13:56
  • @MacLCM I also would recommend you to read "Effective Modern C++", it's full of trick's you can use. And Scott Meyers writing is easy to read. – zilleplus Jun 08 '21 at 21:00

0 Answers0