Public Group

# Stupid error in declaring an instance of a class

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

## Recommended Posts

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?

##### Share on other sites
try putting something like:

#ifndef __MODEL_RESROUCES__
#define __MODEL_RESOURCES__
#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.

##### Share on other sites
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...

##### Share on other sites
Quote:
 Original post by ShaminoWrap that around what code?Where I declare Models?Where I code the ModelResources class?

You would wrap that around the header file.

##### Share on other sites
Quote:
 Original post by ShaminoWrap that around what code?

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

CM

##### Share on other sites
#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)

##### Share on other sites
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.

##### Share on other sites
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...

##### Share on other sites
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.

##### Share on other sites
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.

1. 1
2. 2
3. 3
Rutin
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633726
• Total Posts
3013573
×