I have a school assignment, and it is getting pretty big. And I keep getting errors like Object all ready defind in ***.obj. I guess this is because that more then one .h file is including the same class. And even if I use Include guards I am getting this error. I have heard that forward declarations can help me get rid of these problems. Anyone know how to use them properly. Like now the poblem is that I have a main file, that includes a terrain.h file, and the terrain.cpp file aslo includes the terrain.h file, and then the terrain.h file includes the file.h.
Then I am getting the error:
error LNK2005: "public: __thiscall cFile::cFile(void)" (??0cFile@@QAE@XZ) already defined in main.obj
and so on for the rest of the functions in the cFile class (in the file.h)
Last edited by NoteMe; Nov 11th, 2004 at 12:34 PM.
The .h file doesn't include the Definition of the class does it?
When your thread has been resolved please edit the original post in the thread ()
and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.
When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.
Originally posted by Electroman The .h file doesn't include the Definition of the class does it?
What .h file, and what definition?
The file.h includes the file I/O class, and it's functions, then the terrain.h is only the terrain class definition, and the terrain.cpp is the functions, and then the main.cpp is just Init and Game loop functions and so on.
Well at least it works if I add everything to the terrain.h file, and don't use a terrain.cpp file. Then it works. But I can't understand why I can't include terrain.h from both main and terrain.ccp. Can't say that it looks like it is circular...
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
Not sure if this is the problem, but if you declare the functions in the header, they get declared twice (each time you do #include header.h). This could cause the error you're getting.
You should define the class (leave the function declarations as prototypes, then have another cpp file, where you implement the class.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
So all includes should actualy be in the CPP file rather then the H file?`Ohhhh why have I always done it in the H file. But what if I am using stuff in the H file that needs to be included from an other file, will it still work if I just include it from the CPP file?
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
Originally posted by NoteMe So all includes should actualy be in the CPP file rather then the H file?`Ohhhh why have I always done it in the H file. But what if I am using stuff in the H file that needs to be included from an other file, will it still work if I just include it from the CPP file?
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
You may and should have #include statements in header files, for declarations you depend on. To rely on the .cpp files to include them in the right order is nothing short of madness.
Code:
void foo(); // Function prototype, declaration, to header.
extern int g_whee; // Variable declaration, to header.
class bang; // Class declaration, to header.
class bang // Class definition, still to header.
{
int ding_; // Member declaration.
static int pfft_; // Static member declaration.
public:
bang(); // Member declaration.
bang(int i) : ding_(i) {} // Inline member definition, static linkage, header is fine.
};
void foo() // Function definition, to module.
{
}
int g_whee; // Variable definition, to module.
bang noise; // Variable definition, to module.
int bang::pfft; // Variable definition, to module.
bang::bang() { // Function definition, to module.
}
Template stuff always goes into headers.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Originally posted by CornedBee You may and should have #include statements in header files, for declarations you depend on. To rely on the .cpp files to include them in the right order is nothing short of madness.
Template stuff always goes into headers.
So what do you suggest? It is still not working as long as I have a cpp and a h file for the terrain, and both terrain.cpp and main.cpp is including them.
I suggest your remove all function and variable definitions from your headers. I suggest you make extra-sure that you're not #including any .cpp files anywhere.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
I am sorry....but I get a bit frustrated here. WHen you say definitions do you mean the prototypes or do you mean the code in the functions?
Am I only supposed to have headers if I have a class definition, and not use headers if I only have a set of functions that I want to "encapsulate" in one file, so I don't loose them?
192.168.0.1 Preferred Animal: Penguin Reason for errors: Line#38
Posts
3,051
Originally posted by CornedBee To rely on the .cpp files to include them in the right order is nothing short of madness.
Quotes:
"I am getting better then you guys.." NoteMe, on his leet english skills.
"And I am going to meat her again later on tonight." NoteMe "I think you should change your name to QuoteMe" Shaggy Hiker, regarding NoteMe
"my sweet lord jesus. I've decided never to have breast implants" Tom Gibbons
Originally posted by CornedBee The function prototypes go into headers, the function bodies into modules (.cpp).
If it then still doesn't work, zip the project and upload it, I'll fix it.
Thanks....It doesn't work....the project is pretty big. Let me have some minutes on my hand, And I will reacreat the problem in a smaller project. Then it will be easier for you to find the problem, and I can change the real project.
Thanks a lot....I am getting a friend of mine to mail it to me from school as we speak.
I had the same problem a while back and I think this fixed it.
Code:
// in terrain.h
#ifndef TERRAIN_H
#define TERRAIN_H
// other #includes here
class terrain
{
// class prototypes and members
};
#endif // TERRAIN_H
and in terrain.cpp and main.cpp just #include "terrain.h"
That should fix the 'already defined in main.obj' error.
{EDIT}
Actually I think what is happening is what has already been pointed out - you are trying to define a method in main.cpp which is already defined in terrain.cpp.
Anyway, I think you get a compile error instead of a build error without #include guards.
Yeah I know...thats why I am using include guards. But when they failed. ThenI didn't see what was wrong and started to ask about forward declarations....because I have heard they can solve problems like this:
A variable declaration goes into the header, if you want to use it in more than one file. However, be aware that this:
camera my_camera;
is a definition.
This is a declaration:
extern camera my_camera;
and, as all declarations, it requires exactly one corresponding definition in a .cpp file.
All the buzzt CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
You know I can't seem to recreate that problem at all.
THe only difference I can find is that all my header files are in a directory called HEADER and all my source files are in a directory called SOURCE
Then in the Directories options under the Include section I added the path to my HEADER dir.
Now any include I want I can just
#include <clsCrap.hpp>
I played around trying to get your error, and I am including several includes in many many places with no errors.
The only other differnce is all my headers are .hpp, not .h
EDIT: I went as far to add this to at least 4 different header files
#include <clsForm.hpp>
#include <clsStatic.hpp>
#include <clsCmdButton.hpp>
#include <clsListBox.hpp>
Those are only suppose to be in my Main.cpp
I have NO main.h
And only once did I get an error and i "redefinition class" error.
Not the standard object already defined error...but an error which read " redifinition of 'class' "...weird.
Last edited by Halsafar; Nov 11th, 2004 at 11:26 AM.
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
OK...then that must have been the poblem. I liked it better when I had all my varable/struct/class definitions in the .h file. But it works if I am defining them in the cpp file. You can have a look here if you want to see what I did.
Moving the two lines:
float *zHeight;
BMPHeader filHeader;
from terrain.h to terrain.cpp solved the problem. So I am always supposed to have them in the cpp file then. Even if they are used as parameters in the function prototypes in the h file.
Variables defined in the cpp file?
say what?
that can't be....Out of all the books I've read, not once have they ever put the member varables into the cpp file?
Unless these are not member variables you are refering to.
"From what was there, and was meant to be, but not of that was faded away." - - Steve Damm
"The polar opposite of nothingness is existance. When existance calls apon nothingness it shall return to nothingness." - - Steve Damm
"When you do things right, people won't be sure if you did anything at all." - - God from Futurama
Originally posted by Halsafar Variables defined in the cpp file?
say what?
that can't be....Out of all the books I've read, not once have they ever put the member varables into the cpp file?
Unless these are not member variables you are refering to.
Terrain is not a class. I only have one terrain, so I didn't botter wrapping it up for this "small" assignment. So CornedBee is right. It is not a class.
Hehehe...well right now, when I have used the delete button on more or less everything I guess it looks kind of funny, and there would be million of things to pick on.
If we agree that global variable definitions should always be in the cpp file rather then the h file. THen I have at least solved this problem. Even if I hate it. I want them on the same place as my function prototypes. Well can't have it all.
So you don't need ot download it. Unless you realy want to. It is nothing to run anymore after I deleted most of it. So it is just plain silly code.