OOP class problems

Started by
6 comments, last by Emmanuel Deloget 17 years, 3 months ago
hi all, I'm still new to c++, actually started crossing from VB to c++ couple weeks ago, but I have managed to write my own script engine, nearly done OpenGL engine, and currently am writing GUI system. But I run across a problem that has been bugging me for hours now: I have planned a system like this: in words: 1.) game can access: OpenGL class, script class and GUI class 2.) OpenGL can access: it's own classes (mesh class, light class ...), but that's not important 3.) Script can access: GUI class - to add GUI controls, to update it... 4.) GUI can access: Script class - to rise events(of clicked buttons for example...) and OpenGL - to render it self I have attempted to do this but have failed :( , I hope you can give me some kind of advice, thanks in advance!
Advertisement
Help us out by telling us whats wrong :P

You have failed ... but at what?
You get compiler errors?
You don't know how to create the classes themselves?
Your #include errors?

So on and so forth. Ask a specific question and I'm sure someone can help. Just say you failed and there are so many possible answers its hard to know what you actually want.
Quote:Original post by xadais
I have attempted to do this but have failed :( , I hope you can give me some kind of advice, thanks in advance!


I advise you to read How To Ask Questions The Smart Way, by Eric Steven Raymond. Then, improve upon your question in a manner that lets us actually give you the specific advice relevant to your specific problem in question that you specifically want answered.
class g_obj
{
public:
float x;
float y;
float width;
float height;

int mode;

bool g_click;
bool g_check;
bool g_text;
bool g_movex;
bool g_movey;
bool g_mouse_in;
bool g_mouse_out;
bool is_master;

bool focused;

g_obj();

vector<int> sub_obj;

};
vector<g_obj> gui_objs;
int add_gui_obj(int dm)
{
g_obj tmp;
gui_objs.push_back(tmp);
if(dm!=0) gui_objs[dm].sub_obj.push_back(((int)gui_objs.size()-1));
return ((int)gui_objs.size()-1);
}

this is the class of an gui object, that has child objects, all of gui objects are stored in a vector so they can be easily retrieved by index. i would need be able to call add_gui_obj and access gui_objs from both "game" and "script" class, but when i try to include the in both it gives me an error that the gui_obj and add_gui_obj are defined twice, but that shouldn't happen since i added "#ifndef #define #endif" routine.

the error messages:
razorGL.obj : error LNK2005: "int __cdecl add_gui_obj(int)" (?add_gui_obj@@YAHH@Z) already defined in main.obj
razorGL.obj : error LNK2005: "class std::vector<class g_obj,class std::allocator<class g_obj> > gui_objs" (?gui_objs@@3V?$vector@Vg_obj@@V?$allocator@Vg_obj@@@std@@@std@@A) already defined in main.obj
razorGL_GUI.obj : error LNK2005: "int __cdecl add_gui_obj(int)" (?add_gui_obj@@YAHH@Z) already defined in main.obj
razorGL_GUI.obj : error LNK2005: "class std::vector<class g_obj,class std::allocator<class g_obj> > gui_objs" (?gui_objs@@3V?$vector@Vg_obj@@V?$allocator@Vg_obj@@@std@@@std@@A) already defined in main.obj
razorGL.obj : error LNK2005: "int __cdecl add_gui_obj(int)" (?add_gui_obj@@YAHH@Z) already defined in main.obj
razorGL.obj : error LNK2005: "class std::vector<class g_obj,class std::allocator<class g_obj> > gui_objs" (?gui_objs@@3V?$vector@Vg_obj@@V?$allocator@Vg_obj@@@std@@@std@@A) already defined in main.obj
razorGL_GUI.obj : error LNK2005: "int __cdecl add_gui_obj(int)" (?add_gui_obj@@YAHH@Z) already defined in main.obj
razorGL_GUI.obj : error LNK2005: "class std::vector<class g_obj,class std::allocator<class g_obj> > gui_objs" (?gui_objs@@3V?$vector@Vg_obj@@V?$allocator@Vg_obj@@@std@@@std@@A) already defined in main.obj
Debug/razorengine_cpp.exe : fatal error LNK1169: one or more multiply defined symbols found
rarirurero.
You have a circular dependency between the script class and the GUI class -> bad design.
This is likely because you are defining objects in header files, and then including those header files in multiple places, leading to the same code being input to the linker from more than one object file. In order to correct this, you need to ensure that you have only declarations (not definitions) in your header files, and the corresponding definitions need to be in just one place. Any good C++ text will explain how to do this.
Quote:Original post by Fred304
You have a circular dependency between the script class and the GUI class -> bad design.


To clarify what Fred304 means, your design doen't obey the acyclic dependency principle.

Regards,

This topic is closed to new replies.

Advertisement