W-DOGG Posted June 17, 2009 Share Posted June 17, 2009 (edited) First off: no I'm not missing any libraries. --Please read my second post (in this topic) first for a simplified version of my problem-- Basically here's the situation: I have a class called "DObjects" which will contain and manage a list of Objects added. So hence there is a class "DObject", which will allow basic functions like MoveObject, SetColor, etc. So far, there are 3 types of struct which a "DObject" can inherit, rectangle, circle, and background. Here's what I've got so far for that: Classes code: // DObject CLASS class DObject{ protected: int ObjID; public: DObject SetObject(int type, int NewID){ this->ObjID = NewID; return *this; } int GetObjectID(){ return this->ObjID; } }; //Rectangle struct derative of DObject CLASS #define IDRECT 0 struct DORectangle : DObject{ DORectangle(); int x1; //left int y1; //top int x2; //right int y2; //bottom }; //Circle struct derative of DObject CLASS #define IDCIRC 1 struct DOCircle : DObject{ DOCircle(); int x; //left int y; //top int radius; }; //Background struct derative of DObject CLASS #define IDBKGD 2 struct DOBackground : DObject{ DOBackground(); RGBQUAD color; }; //DObjects CLASS - will manage a list of all added objects class DObjects{ protected: int numObjs; DObject *Objects[65535]; DObject *TempCreateObject; public: DObjects(){ numObjs = -1; return; } DObject *AddObject(int type){ numObjs++; switch(type){ case IDRECT:{ DORectangle *TempCreateObject = new DORectangle; //<--- presumably causing link error } case IDCIRC:{ DOCircle *TempCreateObject = new DOCircle; //<--- presumably causing link error } case IDBKGD:{ DOBackground *TempCreateObject = new DOBackground; //<--- presumably causing link error } } TempCreateObject->SetObject(type, numObjs); Objects[numObjs] = TempCreateObject; return Objects[numObjs]; } void RemoveObject(DObject &RemObj){ int RemID = RemObj.GetObjectID(); delete &Objects[RemID]; return; } }; Now this doesn't bring me any errors, neither when compiling nor when linking. However, when I add the following piece of code anywhere in my project to make use of the classes, I get a link error. Using the classes: DObjects *GameObjects = new DObjects(); DObject *Square = GameObjects->AddObject(IDRECT); The linker gives me the following 3 errors: Link errors: 1>threads.obj : error LNK2019: unresolved external symbol "public: __thiscall DOBackground::DOBackground(void)" (??0DOBackground@@QAE@XZ) referenced in function "public: class DObject * __thiscall DObjects::AddObject(int)" (?AddObject@DObjects@@QAEPAVDObject@@H@Z) 1>threads.obj : error LNK2019: unresolved external symbol "public: __thiscall DOCircle::DOCircle(void)" (??0DOCircle@@QAE@XZ) referenced in function "public: class DObject * __thiscall DObjects::AddObject(int)" (?AddObject@DObjects@@QAEPAVDObject@@H@Z) 1>threads.obj : error LNK2019: unresolved external symbol "public: __thiscall DORectangle::DORectangle(void)" (??0DORectangle@@QAE@XZ) referenced in function "public: class DObject * __thiscall DObjects::AddObject(int)" (?AddObject@DObjects@@QAEPAVDObject@@H@Z) I tried to figure out why, but I have no idea... Can anyone bring light to why this is happening? TA, Wouter Edited June 17, 2009 by W-DOGG Link to comment Share on other sites More sharing options...
0 W-DOGG Posted June 17, 2009 Author Share Posted June 17, 2009 To make it simpler: class A{ }; struct B : A{ B(); }; void callForLinkError(){ B *var = new B; return; } Results into this Link Error: main.obj : error LNK2019: unresolved external symbol "public: __thiscall B::B(void)" (??0B@@QAE@XZ) referenced in function "void __cdecl callForLinkError(void)" (?callForLinkError@@YAXXZ) Link to comment Share on other sites More sharing options...
0 code.kliu.org Posted June 17, 2009 Share Posted June 17, 2009 You are declaring a constructor but not providing one. Link to comment Share on other sites More sharing options...
0 Andre S. Veteran Posted June 17, 2009 Veteran Share Posted June 17, 2009 Your constructors in DOCircle and DORectangle are explicitely declared, but they don't have an implementation. When the linker tries to find the body of these constructors, it fails. Either provide an implementation or remove the declaration to fix the problem. Link to comment Share on other sites More sharing options...
0 W-DOGG Posted June 17, 2009 Author Share Posted June 17, 2009 Cheers guys, didin't know that a body was actually required somewhere after declaring one. Link to comment Share on other sites More sharing options...
Question
W-DOGG
First off: no I'm not missing any libraries.
--Please read my second post (in this topic) first for a simplified version of my problem--
Basically here's the situation:
I have a class called "DObjects" which will contain and manage a list of Objects added.
So hence there is a class "DObject", which will allow basic functions like MoveObject, SetColor, etc.
So far, there are 3 types of struct which a "DObject" can inherit, rectangle, circle, and background.
Here's what I've got so far for that:
Classes code:
// DObject CLASS class DObject{ protected: int ObjID; public: DObject SetObject(int type, int NewID){ this->ObjID = NewID; return *this; } int GetObjectID(){ return this->ObjID; } }; //Rectangle struct derative of DObject CLASS #define IDRECT 0 struct DORectangle : DObject{ DORectangle(); int x1; //left int y1; //top int x2; //right int y2; //bottom }; //Circle struct derative of DObject CLASS #define IDCIRC 1 struct DOCircle : DObject{ DOCircle(); int x; //left int y; //top int radius; }; //Background struct derative of DObject CLASS #define IDBKGD 2 struct DOBackground : DObject{ DOBackground(); RGBQUAD color; }; //DObjects CLASS - will manage a list of all added objects class DObjects{ protected: int numObjs; DObject *Objects[65535]; DObject *TempCreateObject; public: DObjects(){ numObjs = -1; return; } DObject *AddObject(int type){ numObjs++; switch(type){ case IDRECT:{ DORectangle *TempCreateObject = new DORectangle; //<--- presumably causing link error } case IDCIRC:{ DOCircle *TempCreateObject = new DOCircle; //<--- presumably causing link error } case IDBKGD:{ DOBackground *TempCreateObject = new DOBackground; //<--- presumably causing link error } } TempCreateObject->SetObject(type, numObjs); Objects[numObjs] = TempCreateObject; return Objects[numObjs]; } void RemoveObject(DObject &RemObj){ int RemID = RemObj.GetObjectID(); delete &Objects[RemID]; return; } };Now this doesn't bring me any errors, neither when compiling nor when linking.
However, when I add the following piece of code anywhere in my project to make use of the classes, I get a link error.
Using the classes:
The linker gives me the following 3 errors:
Link errors:
I tried to figure out why, but I have no idea...
Can anyone bring light to why this is happening?
TA, Wouter
Edited by W-DOGGLink to comment
Share on other sites
4 answers to this question
Recommended Posts