Stupid error in declaring an instance of a class

Started by
9 comments, last by Shamino 18 years, 4 months ago
Where can I put an instance of my ModelResources class so that everything can access it, but I don't get errors?

class ModelResources
{
public:
	MS3DModel *Model1;
};
if I put ModelResources Models right after that code, it gives me these errors

--------------------Configuration: NeHeGL - Win32 Debug--------------------
Compiling...
NeHeGL.cpp
c:\documents and settings\jonathan\desktop\renderengine\resourcemanager.h(4) : error C2011: 'ModelResources' : 'class' type redefinition
c:\documents and settings\jonathan\desktop\renderengine\resourcemanager.h(9) : error C2086: 'Models' : redefinition

I'm pretty sure I'm not declaring Models anywhere else, why is this happening?
----------------------------------------------------------Rating me down will only make me stronger.----------------------------------------------------------
Advertisement
try putting something like:

#ifndef __MODEL_RESROUCES__
#define __MODEL_RESOURCES__
// your code here
#endif

Really though, its a bad idea to have a class that can be accessed all over the place but if you really do need one. I would advise at looking into the singleton pattern, unless you intend to create more then one object.
- GDKnight
Wrap that around what code?

Where I declare Models?

Where I code the ModelResources class?

Well I really only want a couple files to access the Models class, where I load all my models and where I define my monster's render data...
----------------------------------------------------------Rating me down will only make me stronger.----------------------------------------------------------
Quote:Original post by Shamino
Wrap that around what code?

Where I declare Models?

Where I code the ModelResources class?

You would wrap that around the header file.
- GDKnight
Quote:Original post by Shamino
Wrap that around what code?

All of your headers should have such a construct. See here for an excellent explanation as to why.

CM
#include "MS3D.H"	#ifndef RESOURCE_MANAGER_H#define RESOURCE_MANAGER_Hclass ModelResources{public:	MS3DModel *Model1;};ModelResources Models;#endif


Gives me these errors

--------------------Configuration: NeHeGL - Win32 Debug--------------------Compiling...NeHeGL.cppLinking...Renderer.obj : error LNK2005: "class ModelResources Models" (?Models@@3VModelResources@@A) already defined in NeHeGL.objScene_Object_Manager.obj : error LNK2005: "class ModelResources Models" (?Models@@3VModelResources@@A) already defined in NeHeGL.objWorld.obj : error LNK2005: "class ModelResources Models" (?Models@@3VModelResources@@A) already defined in NeHeGL.objDebug/NeHeGL.exe : fatal error LNK1169: one or more multiply defined symbols foundError executing link.exe.Creating browse info file...NeHeGL.exe - 4 error(s), 0 warning(s)
----------------------------------------------------------Rating me down will only make me stronger.----------------------------------------------------------
That is most likely being caused by the way your linking your files. Examine the ways you are linking your files headers together, without any more information, I don't really know what to tell you. This is really a class design issue, it could be in any numerous amount of files.
- GDKnight
I don't care if you think its a stupid class, but for right now its my solution for not having a resource manager :D.

Do you just need like, what I'm including where?

Figured it out, not all of my files have ifndef's in them...
----------------------------------------------------------Rating me down will only make me stronger.----------------------------------------------------------
Pretty much where your including files in each file, and perhaps if your using the macros I showed you to use in your header files. Simliar to the same thing that Conner McCloud showed you with his article.
- GDKnight
You are getting those errors because you declared a variable in the header file that wasn't static or extern. This means each translation unit (.cpp file) is making a copy of it, and all of a sudden you've got multiple symbols.

For a quick and easy fix, you can define it as extern:
// in your header file extern ModelResources Models;// in only ONE source file:ModelResources Models;

You require the second part of that because you need to initiate the variable with something. A more complex answer isn't really necessary, unless you really want it.

However, whilst it's a quick and easy fix, it's pretty hacky. Learn up on Monostates and possibly think of other ways to structure the code so you don't have this problem. Hope that helps.
[ search: google ][ programming: msdn | boost | opengl ][ languages: nihongo ]

This topic is closed to new replies.

Advertisement