Skip to main content
GameDev.net gamedev.net
🔒 Locked

Inheritance Woes

Started by Sloner Dec 20, 2005 at 11:01 PM 2 replies 550+ views
Original Post
Sloner
Sloner
I'm having a little trouble with inheritance. I haven't used it much in C++ before, so the arrangements are a little strange to me. I've set up an Object class with two virtual functions, animate(int dt) and draw() as follows: public: virtual void draw(); virtual void animate(int dt); I've definied both of these functions in my SolidObject class, which inherits from Object. The code looks like this: class SolidObject : public Object { ....blahblah.... public: void draw() { m_sprite->draw(m_x, m_y); }; void animate(int dt) { m_sprite->animate(dt); }; ....blahblah.... }; When I try to compile, I get the following errors: SlonerOpenGL.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Object::animate(int)" (?animate@Object@@UAEXH@Z) SlonerOpenGL.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Object::draw(void)" (?draw@Object@@UAEXXZ)
Rycross
Rycross
Virtual functions still have to be defined in the base class. If you want to not implement the functions in the base class, then you need to make them pure virtual. You do this by adding "=0" at the end of the function definition. For example: virtual void draw()=0;
demonkoryu
demonkoryu
Either declare your virtual methods in Object as pure virtual, like
virtual void draw() = 0;
or supply an (empty) implementation for them.

Edit: Beaten to the punch
Sloner
Sloner
You rock! I remember reading that somewhere before, now that you mention it. It's been a while, though.

Thanks a bunch!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.