Sign in to follow this  
Waterfox

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 this post


Link to post
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 )
{
}


Or is your code different?

Share this post


Link to post
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by Waterfox
I 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by blackbird04217
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).


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

Share this post


Link to post
Share on other sites
Quote:
Original post by Waterfox
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?


Yes.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this