Follow us on Twitter!
Never in the field of human conflict was so much owed by so many to so few. - Winston Churchill
Wednesday, April 23, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 22
Guests Online: 21
Members Online: 1

Registered Members: 82885
Newest Member: ConiBE
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

C++ undefined symbols


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 10-12-09 00:31
I am working on the internal logic of a simple game, and I can't build it, I get this:
Code
Undefined symbols:
  "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:
Code
Undefined symbols:
  "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:
Code
~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:
Code
Player::Player(Point, int, dir)in ccPAFOip.o



the constructor is calling the base class's destructor?
here's the constructor.
Code
explicit Player(Point loc=Point(0,0), int slvl=1,dir d=E){
    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?
Author

RE: C++ undefined symbols

p4plus2
Member

Your avatar

Posts: 167
Location:
Joined: 31.03.08
Rank:
Newbie
Posted on 10-12-09 04:49
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:
Code

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.


"You can't be something your not,
Be yourself by yourself
Stay away from me" ~Walk, Pantera

"Playing an acoustic guitar is like having sex with your clothes on" ~Dave Mustaine
p4plus2@hotmail.com
Author

RE: C++ undefined symbols


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 10-12-09 20:01
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...

Edited by on 10-12-09 20:03