In class sample program demonstrating header files won't compile?

Hey guys,

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.


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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
 // 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"

using namespace 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"

using namespace 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.

Thanks.

Last edited on
you should include the header file c1.h in c2.cpp instead of the implementation file
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)?
Last edited on
You only need to change this :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// START OF MAIN PROGRAM FILE CALLED "c2.cpp"
#include "c1.h" // <<<==

using namespace 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.
Ohhhh! Is it because both the header file and the implementation files have the same name "c1" just different extensions?
You have the source (cpp) files together in the project, so they will be linked together. It has nothing to do with their names.
Oh ok, thanks!
Topic archived. No new replies allowed.