• Advertisement
Sign in to follow this  

Virtual function and linking error

This topic is 4384 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have got a hierarchy in my game's classes: .........BaseClass ............| ..........Shape-- .............|......| ...........Line...Circle etc. Render function is declared as virtual in 'BaseClass', it is derived in other classes and used there as well. I introduce another function in 'Shape' class: void Render(LPDIRECT3DVERTEXBUFFER9 pVB ); This function is present in all classes derived from BaseClass. Code compiles and links just fine. If I change Render function of 'Shape' class to virtual: virtual void Render(LPDIRECT3DVERTEXBUFFER9 pVB ); I start getting linker errors that go something like:
Container.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall UserInterface::Line::Render(struct IDirect3DVertexBuffer9 *)" (?Render@Line@UserInterface@@UAEXPAUIDirect3DVertexBuffer9@@@Z)
Map.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall UserInterface::Line::Render(struct IDirect3DVertexBuffer9 *)" (?Render@Line@UserInterface@@UAEXPAUIDirect3DVertexBuffer9@@@Z)
ShapesMan.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall UserInterface::Line::Render(struct IDirect3DVertexBuffer9 *)" (?Render@Line@UserInterface@@UAEXPAUIDirect3DVertexBuffer9@@@Z)
Container.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall UserInterface::MultiArc::Render(struct IDirect3DVertexBuffer9 *)" (?Render@MultiArc@UserInterface@@UAEXPAUIDirect3DVertexBuffer9@@@Z)
ShapesMan.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall UserInterface::MultiArc::Render(struct IDirect3DVertexBuffer9 *)" (?Render@MultiArc@UserInterface@@UAEXPAUIDirect3DVertexBuffer9@@@Z)
Container.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall UserInterface::Path::Render(struct IDirect3DVertexBuffer9 *)" (?Render@Path@UserInterface@@UAEXPAUIDirect3DVertexBuffer9@@@Z)
ShapesMan.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall UserInterface::Path::Render(struct IDirect3DVertexBuffer9 *)" (?Render@Path@UserInterface@@UAEXPAUIDirect3DVertexBuffer9@@@Z)
Container.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall UserInterface::Zone::Render(struct IDirect3DVertexBuffer9 *)" (?Render@Zone@UserInterface@@UAEXPAUIDirect3DVertexBuffer9@@@Z)
ShapesMan.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall UserInterface::Zone::Render(struct IDirect3DVertexBuffer9 *)" (?Render@Zone@UserInterface@@UAEXPAUIDirect3DVertexBuffer9@@@Z)
Container.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall UserInterface::MotionPlan::Render(struct IDirect3DVertexBuffer9 *)" (?Render@MotionPlan@UserInterface@@UAEXPAUIDirect3DVertexBuffer9@@@Z)
ShapesMan.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall UserInterface::MotionPlan::Render(struct IDirect3DVertexBuffer9 *)" (?Render@MotionPlan@UserInterface@@UAEXPAUIDirect3DVertexBuffer9@@@Z)

As a test, another stand-alone main() type application was created, where some classes were declared: .....Base ......| .....Der1 ......| .....Der2 ......| .... Der3 There is a function Display() in Base as virtual: virtual Display(); It is also derived in all its derived classes. Another function is introduced in Der1 class as virtual: virtual Display2(int x); It is also present in all its derived classes. Now when the code is compiled and run, no linker errors are present. Isnt this similar to the scenario of first example? What is missed? Thank you for the help..

Share this post


Link to post
Share on other sites
Advertisement
You are most likely getting those errors because render isn't defined in BaseClass, you need to either declare it pure virtual like this
virtual void Render(LPDIRECT3DVERTEXBUFFER9 pVB ) = 0;
Or give an empty definition like this:
virtual void Render(LPDIRECT3DVERTEXBUFFER9 pVB ){}
The former will force derived classes to implement Render while with the latter they can choose for themselves.

The reason you didn't get any errors when trying to duplicate the problem was most likely that you didn't create an object of der1, der2 and/or der3. The following test give me errors:

class base
{
public:
base(){}
virtual ~base(){}

virtual void f();
};
class a : public base
{
public:
a(){}
virtual ~a(){}

void f(){}
};

int main()
{
a obj; // Remove this line to get rid of error.
return 0;
}

Share this post


Link to post
Share on other sites
I have checked this, (wrt my original code) I dont have a pure virtual function, just a simple virtual function. And this virtual function is not present in the 'BaseClass', but in 'Shape' class. For all classes derived from 'Shape', and including 'Shape', they both have exactly the same function in their headers, as well as in their source files.

That is to say (again),

virutal void Render(LPDIRECT3DVERTEXBUFFER9 pVB );

is in 'Shape' class only, not in 'BaseClass'.

All derived classes from 'Shape' have

void Render(LPDIRECT3DVERTEXBUFFER9 pVB );

and for each declaration, body of function is in source file for that class as well.

I know its hard to try help something out when u canot see the code..

I really appretiate it..

p.s. i'm in a hurry so pardon my english

Share this post


Link to post
Share on other sites
If you do have a definition for Render in the Shape class then I have no idea what is wrong, when I say definition I mean this in a source file:

void Shape::Render(LPDIRECT3DVERTEXBUFFER9 )
{}

Share this post


Link to post
Share on other sites
Quote:
Original post by Taha Ansari
virutal void Render(LPDIRECT3DVERTEXBUFFER9 pVB );

is in 'Shape' class only, not in 'BaseClass'.

All derived classes from 'Shape' have

void Render(LPDIRECT3DVERTEXBUFFER9 pVB );
You're probably going to have to post code with the non-relevant parts snipped out.

That error looks like the compiler is not able to find the implementation. Maybe there's a typo, or you happened to forgot the classname on the implementation?
class Shape { 
//...
virtual void Render(LPDIRECT3DVERTEXBUFFER9 pVB );
};

class Circle : public Shape {
//...
virtual void Render(LPDIRECT3DVERTEXBUFFER9 pVB );
};

void Shape::Render(LPDIRECT3DVERTEXBUFFER9 pVB ) {
//...
}

void Circle::Render(LPDIRECT3DVERTEXBUFFER9 pVB ) {
//...
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement