problems with multipule files using a global.h

OS: Windows 7
IDE: VS 2010

So I have been searching google for a the last couple of days and can't quite get the answer I have been looking for, so here is to hoping I can here.

My problem is in using multiple files, 1 common header file (global.h) and 2 cpp files (main.cpp and class.cpp), and I keep getting error that reads like this:

1>main.obj : error LNK2005: "public: void __thiscall person::prepStat(void)" (?prepStat@person@@QAEXXZ) already defined in class.obj
1>main.obj : error LNK2005: "public: void __thiscall person::setStat(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,int,float)" (?setStat@person@@QAEXV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@HM@Z) already defined in class.obj
1>main.obj : error LNK2005: "public: void __thiscall person::getStat(void)" (?getStat@person@@QAEXXZ) already defined in class.obj
1>main.obj : error LNK2005: "class person ashley" (?ashley@@3Vperson@@A) already defined in class.obj


global.h:
1
2
3
4
5
6
7
8
9
10
#ifndef GLOBAL_H
#define GLOBAL_H

#include<iostream>
#include<string>

using namespace std;

#include"class.cpp"
#endif 


class.cpp:
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
#ifndef CLASS_CPP
#define CLASS_CPP
#include "global.h"

class person
{
public:
	void prepStat();
	void setStat(string noun, int time, float inches);
	void getStat();

private:
	string name;
	int age;
	float hight;
}ashley;

void person::prepStat()
{
	ashley.setStat("Ashley", 25, 5.5);
}

void person::setStat(string noun, int time, float inches)
{
	name = noun;
	age = time;
	hight = inches;
}

void person::getStat()
{
	cout << name << " is " << age << "years old and " << hight << " inches." << endl;
}
#endif 


main.cpp:
1
2
3
4
5
6
7
8
9
#include "global.h"

int main()
{
	ashley.getStat();

	system("pause");
	return 0;
}


Now I understand that I can put it all in one and just remove global.h however, I am trying to make a more complex problem. I did this just to test out my problems and still can't work it out. So any help would be greatly appreciated and thanks in advance.
You shall remove the line

#include"class.cpp"


from your header file

#ifndef GLOBAL_H
#define GLOBAL_H

#include<iostream>
#include<string>

using namespace std;

#include"class.cpp"
#endif
Thanks for the idea vlad however, I removed the #include "class.cpp" form the global.h and now I get this error:

1> main.cpp
1>c:\users\martsul\documents\vsc++.projects\test\test\main.cpp(5): error C2065: 'ashley' : undeclared identifier
1>c:\users\martsul\documents\vsc++.projects\test\test\main.cpp(5): error C2228: left of '.getStat' must have class/struct/union
1> type is ''unknown-type''


If I add #include "class.cpp" than I get the same error as before. Again any help would be appreciated.
global.h should contain:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef GLOBAL_H
#define GLOBAL_H

#include<iostream>
#include<string>

using namespace std;

class person
{
public:
	void prepStat();
	void setStat(string noun, int time, float inches);
	void getStat();

private:
	string name;
	int age;
	float hight;
};

#endif  


class.cpp should contain:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include "global.h"

void person::prepStat()
{
	ashley.setStat("Ashley", 25, 5.5);
}

void person::setStat(string noun, int time, float inches)
{
	name = noun;
	age = time;
	hight = inches;
}

void person::getStat()
{
	cout << name << " is " << age << "years old and " << hight << " inches." << endl;
}


main.cpp should contain:
1
2
3
4
5
6
7
8
9
10
11
#include "global.h"

person ashley;

int main()
{
	ashley.getStat();

	system("pause");
	return 0;
}


The headers contain forward declarations. The source files contain the class' functions.
Thanks Stewbound I have attempted what you said to do, but now I get the following errors:

1>c:\users\martsul\documents\vsc++.projects\test2\test2\class.cpp(5): error C2065: 'ashley' : undeclared identifier
1>c:\users\martsul\documents\vsc++.projects\test2\test2\class.cpp(5): error C2228: left of '.setStat' must have class/struct/union
1> type is ''unknown-type''


and if I leave the class creation of ashley at the end of the deceleration of class I get my original errors of it being multiply defined. Now say I wanted to make multiple headers could I put the deceleration of class person inside another header and add it to global, given I can fix my current issues? Also could VS be my problem because I have never had this much problem with other compilers.
Last edited on
1
2
3
4
void person::prepStat()
{
	ashley.setStat("Ashley", 25, 5.5);
}

Your class does not know you will create a ashley object.
Make it become this:
1
2
3
4
void person::prepStat()
{
	setStat("Ashley", 25, 5.5);
}


NOT definitely a VS problem. And I suggest you to allow passing the Name as a Parameter:
1
2
3
4
void person::prepStat(string Name)
{
	setStat(Name, 25, 5.5);
}
Last edited on
If you must use a global variable, your class.cpp file must know about it before using it.

In global.h, after line 20, add

extern person ashley;

(You could instead add this line at line 2 of class.cpp)
Also, to better organize your code, consider changing the names of global.h and class.cpp to person.h and person.cpp. Naming your header and source files to align with the class they define make it easier for you and other users of the code to know what each file contains.
It's not needed that extern thing. Ashley is defined in main.
Ok I appreciate the help you guys have given me. EssGeEich your tutilage worked and kind of answed another question I had. Doug4 I will try your idea when I get back of work, the reason why I did this was one narrow down a problem I had in a larger program. Also I had the global there because I had wanted to set up the class in a file seperate from the main.cpp and utilize a global.h again for a larger program that I have because it is beginning to get annoyingly large.
Last edited on
@EssGeEich,

It's not needed that extern thing. Ashley is defined in main.


However, ashley is not declared in class.cpp. That's why it needs to be externed.

I agree that your approach is a better solution, but if globals are to be used, the global must be declared in the file that uses it.
Last edited on
ok I finally got some time to try the exturn and I get the fallowing error:
>main.obj : error LNK2001: unresolved external symbol "class person ashley" (?ashley@@3Vperson@@A)
1>C:\Users\Martsul\Documents\VSC++.Projects\test2\Debug\test2.exe : fatal error LNK1120: 1 unresolved externals


other than this I did not make any changes.
doug4: Ashley is defined in main (it should have been, at least). He simply wanted to set a default name when he calls such function.

Also, rallici, show us your main.cpp file, and change this (
1
2
3
4
5
6
7
8
9
10
11
12
class person
{
public:
	void prepStat();
	void setStat(string noun, int time, float inches);
	void getStat();

private:
	string name;
	int age;
	float hight;
}ashley;
)
into this:
1
2
3
4
5
6
7
8
9
10
11
12
class person
{
public:
	void prepStat();
	void setStat(string noun, int time, float inches);
	void getStat();

private:
	string name;
	int age;
	float hight;
};
)
(Remove "ashley" at the end)
Also put this last code snippet into "class.h" and just include "class.h" into your main.cpp file. It will automatically include class.cpp as far as it's included into your project.
And also show us your class.cpp file.
Last edited on
Topic archived. No new replies allowed.