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
#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.
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.
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.
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.
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.
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
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.