C++ undefined symbols
I am working on the internal logic of a simple game, and I can't build it, I get this:
"vtable for Character", referenced from:
__ZTV9Character$non_lazy_ptr in cc0hBtWa.o
"Character::~Character()", referenced from:
Player::Player(Point, int, dir)in cc0hBtWa.o
Player::~Player()in cc0hBtWa.o
Player::~Player()in cc0hBtWa.o
"typeinfo for Character", referenced from:
typeinfo for Playerin cc0hBtWa.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
when I declare the destructor for the abstract base class Character as pure virtual, I get this:
"Character::~Character()", referenced from:
Player::Player(Point, int, dir)in ccPAFOip.o
Player::~Player()in ccPAFOip.o
Player::~Player()in ccPAFOip.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
the destructor for player looks like this:
markup~Player(){delete location;}
location is a protected data member of Character, Player inherits from Character.
I understand that the destructors could call their base class destructor, maybe (correct me if I'm wrong, I haven't used inheritance before…),
what I don't get is why those symbols are undefined, or this:
markupPlayer::Player(Point, int, dir)in ccPAFOip.o
the constructor is calling the base class's destructor?
here's the constructor.
location=new Point(loc.getX(),loc.getY());
direction=d;
lvl=slvl;
req=10;
atk=defense=speed=5+3*lvl;
health=100.0+(5*lvl);
}```
can anyone explain to me why this won't link?
This is a broad error(as with many linker errors…) but this one usually comes from placing a constructor and destructor in your header but not cpp file. This may be caused by not using the constructor correctly since you have defined it as explicit. When using explicit remember the following:
class A {
public:
explicit A(int);
};
A a1 = 100; // bad
A a2 = A(147); // good
A a3(258); // good
Also, if you have an unnamed function in your header file, (for example "bool (int)") that could cause this type of linker error.
Overall the way to go about solving this is check function names, check your spelling, make sure datatypes match, try removing the explicit keyword(it may be the problem), if all else fails post a bit more of your code so people can take a better look at it.
Thank you. I figured out the solution: I removed the declaration and definition for the destructor from the Player class, and Enemy class, and defined it in the Character base class, and it worked. Now I know derived class destructors can't delete dynamically allocated protected data members from the base class, the base class needs its destructor to do that… I think I may have learned more about OOP in the past 2 days than I ever have since I started learning to program…