# Stupid error in declaring an instance of a class

This topic is 4377 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.

##### Share on other sites
Okay Lets start from my CPP files up

MS3d.cpp

#include <windows.h>		// Header File For Windows#include <fstream.h>            // header for fstream #include "TextureManager.h"     // header for tex loading#include "MS3D.h"               // header for ms3d

NeHeGL.cpp

#include <windows.h>                                                                 // Header File For The Windows Library #include "NeHeGL.h"                                                                      // Header File For The NeHeGL Basecode #include "Renderer.h"     // for mah render engine#include "ResourceManager.h"    // for my resource manager

Renderer.cpp
#include "NeHeGL.h"	 // for nehegl framework #include "Renderer.h"	 // for the renderer

Scene_Object_Manager.cpp
#include "Scene_Object_Manager.h" // for scene object code

Vector3.cpp
#include "Vector3.h"

MS3D.h
#include <windows.h>	// for windows			#include <gl\glaux.h>	// for opengl aux		#include <vector> // for vector stuff

NeheGL.h
#include <windows.h>								// Header File For Windows

Object.h
#include "MS3D.h"#include "ObjectFactory.h"#include "ResourceManager.h"

ObjectFactory.h
#include <map>#include "MacroRepeat.h"

Renderer.h
#include "Vector3.h"#include "Scene_Object_Manager.h"

ResourceManager.h
#include "MS3D.H"

Scene_Object_Manager.h
#include "Object.h"

TextureManager.h
#include <gl\glaux.h>											// Header File For The GLaux Library#include <stdio.h>

Vector3.h
nothing here

ALL header files have ifdef's on them....

I made the ModelResources Models an extern and declared it in a CPP file...

Fixed it right up, but is this the right way to do it?

[Edited by - Shamino on December 17, 2005 11:16:22 PM]

##### Share on other sites

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

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
628651
• Total Posts
2984044

• 10
• 9
• 9
• 10
• 21