In my C++ course we have just started dealing with programs that are comprised of a header, implementation and a main file. I am trying to get a sample program I was given in class to compile so I can use it as a template for the actual assignment on this topic I have to complete. Something isn't working quite right and while I imagine the mistake I have made is very simple I am unable to find a typo. I also cannot seem figure out what the error printout is telling me.
// START OF HEADER FILE CALLED "c1.h"
class counter
{
public:
int currentcount();
void addone();
void subone();
counter();
counter(int);
private:
int cur_count;
};
// END OF HEADER FILE
// START OF IMPLEMENTATION FILE CALLED "c1.cpp"
#include <iostream>
#include "c1.h"
usingnamespace std;
int counter::currentcount()
{
return cur_count;
}
void counter::addone()
{
cur_count++;
}
void counter::subone()
{
cur_count--;
}
counter::counter()
{
cur_count = 0;
}
counter::counter(int i)
{
cur_count = i;
}
// END OF IMPLEMENTATION FILE
// START OF MAIN PROGRAM FILE CALLED "c2.cpp"
#include "c1.cpp"
usingnamespace std;
int main()
{
counter c1;
cout << c1.currentcount() << endl;
c1.addone();
c1.addone();
cout << c1.currentcount() << endl;
return 0;
}
// END OF MAIN PROGRAM FILE
I am attempting to run this program in Visual Studio 2013 on a Windows 7 machine. I'm not sure if I am doing this correctly or not but in Visual Studio I have created a project and created c1.h in the project subfolder "Header Files", c1.cpp in "Resource Files" and c2.cpp in "Source Files". These are all listed on the left of my interface in a box titled "Solution Explorer". Hopefully that's how it's supposed to be done, anyway the error readout I am currently getting is...
Error 1 error LNK2005: "public: __thiscall counter::counter(int)" (??0counter@@QAE@H@Z) already defined in c1.obj c:\Users\Steve\documents\visual studio 2013\Projects\Project7\Project7\c2.obj Project7
Error 2 error LNK2005: "public: __thiscall counter::counter(void)" (??0counter@@QAE@XZ) already defined in c1.obj c:\Users\Steve\documents\visual studio 2013\Projects\Project7\Project7\c2.obj Project7
Error 3 error LNK2005: "public: void __thiscall counter::addone(void)" (?addone@counter@@QAEXXZ) already defined in c1.obj c:\Users\Steve\documents\visual studio 2013\Projects\Project7\Project7\c2.obj Project7
Error 4 error LNK2005: "public: int __thiscall counter::currentcount(void)" (?currentcount@counter@@QAEHXZ) already defined in c1.obj c:\Users\Steve\documents\visual studio 2013\Projects\Project7\Project7\c2.obj Project7
Error 5 error LNK2005: "public: void __thiscall counter::subone(void)" (?subone@counter@@QAEXXZ) already defined in c1.obj c:\Users\Steve\documents\visual studio 2013\Projects\Project7\Project7\c2.obj Project7
Error 6 error LNK1169: one or more multiply defined symbols found c:\users\steve\documents\visual studio 2013\Projects\Project7\Debug\Project7.exe 1 1 Project7
Any help on what I am missing here would be greatly appreciated. I just need to be able to run a program that is split up into multiple files so I can get the code for my actual assignment to work.
I tried doing what you suggested but I must not be understanding you correctly. Would you mind listing off what you think the #include statements should be for each of my files (c1.h : c1.cpp : c2.cpp)?
// START OF MAIN PROGRAM FILE CALLED "c2.cpp"
#include "c1.h" // <<<==
usingnamespace std;
int main()
{
counter c1;
cout << c1.currentcount() << endl;
c1.addone();
c1.addone();
cout << c1.currentcount() << endl;
return 0;
}
// END OF MAIN PROGRAM FILE
Thanks that worked! I am still a bit confused as to how the compiler knows to go and get c1.cpp though? There is no longer a #include "c1.cpp" statement in any of the files.