Using if_ndef with cpp files

So I have this class in main that I want to use in another file Square.cpp. However, I include Square.h in main.cpp. So I put an ifndef define endif block around main.cpp and Square.h to prevent multiple compilations of the same file. For instance main.cpp looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#ifndef main_cpp
#define main_cpp

#include <string>
#include <vector>
#include <iostream>
#include "glut.h"  // Include glut/glu/glu definitions
#include "Square.h"
#include "Direction.h"

using namespace std;

class World
{
public:
	static float W_L;
	static float W_B;
	static float W_width;
	static float W_height;
	static float W_R;
	static float W_T;
};

float World::W_L = 0.0;
float World::W_B = 0.0;
float World::W_width = 100.0;
float World::W_height = 100.0;
float World::W_R = World::W_L + World::W_width;
float World::W_T = World::W_B + World::W_height;

int main (int argc, char** argv)
{
...
}

#endif 


Square.h looks like this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef Square_h
#define Square_h

#include <iostream>
#include "Direction.h"
#include <math.h>
#include "glut.h"  // Include glut/glu/glu definitions
#include <string>

using namespace std;

class Square
{
...
};

	
#endif 


and Square.cpp looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include "main.cpp"
#include "Square.h"
#include <string>
using namespace std;

Square::Square ()
{
...
}

...


However, I still get the following compilation error:


Square.obj : error LNK2005: _main already defined in main.obj


Does anyone know what I'm doing wrong?
You should not include the cpp file ( nor guard it )
Add a new header with the declarations of class World and include that

http://www.cplusplus.com/forum/articles/10627/
Ok. I feel like I'm doing something, because I'm being told to do it instead of understanding why I should do it. I read most of the article, but I have a hard time understanding why it's best to create .h files for each class.

The reason why I created the class World in main was because I had some variables in main that I wanted to use in my other class Square. However, variables in main.cpp don't have scope in square.cpp unless you do one of two things:

1. create an extern (which I don't know how to do)

2. create a class and make it's data members static. Since the variables I'm using

W_L, W_B, W_width, ...

are mostly used in main I wanted to keep the class in main. However, since I read the article I've created two more files World.h and World.cpp.

Any thoughts? Should I've created externs instead or am I missing something else. I'm just trying to learn good software design and one of the keys to good software design is single point of control. So if I can make W_L, W_B, ... global to all the files that use it then whenever I need to modify these values I only have to change one line of code.

Thanks for responding to my post
Matthew
Hi Dunsondog109,
We create seperate .H files for every class as that seperates the implementation from the declaration. For a small application it seems easy to have implementation and declaration in the same file, but this is not the case for a commercial application. Many people are working on the same issue and they need to have a concrete definition of things which .H file provides.
If you make the variables as extern then those variables will have scope outside of the class in which they are declared. My advice is not to do that, though I am not THE EXPERT in C++. Instead, you can create an object of class 'main' in class 'square'. That will give you access to the member variables of class Main.
Second thing is, you dont include the .CPP files in other .CPP files. The reason is that compiler creates .OBJ files for each of your .CPP files. Then, the linker looks for the definitions of the methods that are defined, inside this .OBJ files. Now, if you include Main.cpp in Square.CPP, while compiling square.cpp, one more file for Main.CPP (i.e. Main.OBJ) is created, which is nothing but dual definition for the same thing, once on its own and once through main.obj.
So, the best way is to create .H of your classes and include those in your application. The linker will find the definition on its own..you don't need to take care of that in your code by including .CPP in other .CPPs.
Hope this is what you were looking for.
Thanks devacool420
Topic archived. No new replies allowed.