# Defining virtual functions.

## Recommended Posts

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]

##### Share on other sites
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 ){}

##### Share on other sites
From the sound of things, it seems like you didn't declare it. For that see yaustar's post with bBone.h.

##### Share on other sites
Ahh, I see. I didn't declare it in bBone, I thought it would of automatically done that since it was inherited.
Thanks!

##### Share on other sites
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]

##### Share on other sites
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.

##### Share on other sites
It definitely can find the definition, it's just that error pops up as soon as I define bBone's constructor.

##### Share on other sites
Quote:
 Original post by WaterfoxIt 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.

##### Share on other sites
oops, my bad. Everything's fine now, thanks everyone.

##### Share on other sites
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.

##### Share on other sites
Do you have a vector of bones or a vector of pointers to bones?

std::vector<Bone> foo;

or

std::vector<Bone*> foo;

##### Share on other sites
A vector of bones.

##### Share on other sites
Quote:
 Original post by WaterfoxI vector of bones

Well that's why it doesn't do what you expect, its never going to call your bBone::Draw because you're not storing any bBone's in the vector, you're only storing Bone's.

For it to work you'll have to store pointers to Bones in the vector.

##### Share on other sites
Ahh, I get it. So since I'm pushing back new Bones and bBones, when I want to get rid of the vector will I have to iterate delete through the vector?

##### Share on other sites
You seem to have: virtual void Draw(void); in the the Bone class, and this: virtual void Draw(ID2D1SolidColorBrush *Black); in the bBone class, making them two entirely different calls. Likely this is the reason the Bone::Draw is always called. They must match up, such as both being: void Draw(void).

##### Share on other sites
Quote:
 Original post by WaterfoxAhh, I get it. So since I'm pushing back new Bones and bBones, when I want to get rid of the vector will I have to iterate delete through the vector?

Or you use something less error-prone like a Boost pointer container.

##### Share on other sites
Quote:
 Original post by blackbird04217You seem to have: virtual void Draw(void); in the the Bone class, and this: virtual void Draw(ID2D1SolidColorBrush *Black); in the bBone class, making them two entirely different calls. Likely this is the reason the Bone::Draw is always called. They must match up, such as both being: void Draw(void).

No. None of the OP's sample code shows these issues but he clearly slices his objects when inserting them into his container.

##### Share on other sites
Quote:
 Original post by WaterfoxAhh, I get it. So since I'm pushing back new Bones and bBones, when I want to get rid of the vector will I have to iterate delete through the vector?

Yes.

##### Share on other sites
Thanks everyone, hopefully that's the last of my problems on this topic.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628291
• Total Posts
2981862

• 11
• 10
• 10
• 11
• 17