Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
Share on other sites

#include "MS3D.H"

#ifndef RESOURCE_MANAGER_H
#define RESOURCE_MANAGER_H
class ModelResources
{
public:
MS3DModel *Model1;
};

ModelResources Models;

#endif




Gives me these errors


--------------------Configuration: NeHeGL - Win32 Debug--------------------
Compiling...
NeHeGL.cpp
Linking...
Renderer.obj : error LNK2005: "class ModelResources Models" (?Models@@3VModelResources@@A) already defined in NeHeGL.obj
Scene_Object_Manager.obj : error LNK2005: "class ModelResources Models" (?Models@@3VModelResources@@A) already defined in NeHeGL.obj
World.obj : error LNK2005: "class ModelResources Models" (?Models@@3VModelResources@@A) already defined in NeHeGL.obj
Debug/NeHeGL.exe : fatal error LNK1169: one or more multiply defined symbols found
Error executing link.exe.
Creating browse info file...

NeHeGL.exe - 4 error(s), 0 warning(s)

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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"


now for the headers

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 this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this