Defining virtual functions.

Started by
17 comments, last by Waterfox 14 years ago
I have a base class called Bone and an inherited class called bBone. Bone has the virtual function Draw(), but when I go to define Draw() for bBone in the implementation file I get
error C2509: 'Draw' : member function not declared in 'bBone'
This is how I'm defining Draw for bBone
void bBone::Draw(ID2D1SolidColorBrush *Black){
stuff
}

My tutorial has the definition as part of the declaration so I don't know how to do it separately. [Edited by - Waterfox on March 20, 2010 10:37:08 PM]
Advertisement
To make sure we are on the same page, do we have:

Bone.h
class Bone{  public:    virtual void Draw( ID2D1SolidColorBrush *Black );};


bBone.h
class bBone : public Bone{  public:    void Draw( ID2D1SolidColorBrush *Black );};


bBone.cpp
void bBone::Draw( ID2D1SolidColorBrush *Black ){}


Or is your code different?

Steven Yau
[Blog] [Portfolio]

From the sound of things, it seems like you didn't declare it. For that see yaustar's post with bBone.h.
Ahh, I see. I didn't declare it in bBone, I thought it would of automatically done that since it was inherited.
Thanks!
Now I have a new problem, as soon as I define my constructor for bBone I get
1>stick.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall Bone::Draw(void)" (?Draw@Bone@@UAEXXZ)


declarations
class Bone{protected:	ID2D1EllipseGeometry *dot;	float angle;	D2D1_MATRIX_3X2_F matrix;	BOOL hit;public:	virtual void Draw();};class bBone:public Bone{public:	bBone();	void Draw(ID2D1SolidColorBrush *Black);};

definitions
void bBone::Draw(ID2D1SolidColorBrush *Black){stuff}bBone::bBone(){stuff}


[Edited by - Waterfox on March 20, 2010 11:01:34 PM]
I'm pretty sure that means the compiler can't find the definition for bone's draw function.

Make sure you define the Draw function for bone.
"All you have to decide is what to do with the time that is given to you." - Gandalf
It definitely can find the definition, it's just that error pops up as soon as I define bBone's constructor.
Quote:Original post by Waterfox
It definitely can find the definition, it's just that error pops up as soon as I define bBone's constructor.


Quote:I'm pretty sure that means the compiler can't find the definition for bone's draw function.

Make sure you define the Draw function for bone.


Quote:public: virtual void __thiscall Bone::Draw(void)


Bone (the base class), not bBone (the derived class). The base class is not abstract, so it needs definitions too.

The reason you don't see the error until you've defined bBone's constructor is that otherwise it doesn't get that far in the error-checking process.
oops, my bad. Everything's fine now, thanks everyone.
New problem :(
I have a vector of Bones containing a Bone and a bBone, but when I iterate the Draw function through the vector, bBone does Bone's Draw function. I've defined both to have different Draw functions though.

This topic is closed to new replies.

Advertisement