Header File woes

Hello, I have am using Visual Studio 2010 and I am having trouble getting my program to compile due to a linking error.

I was informed that I can link to a header file from main.cpp via #include "test.h", where I would include my class definition, and have another file with the same name "test.cpp" automatically be complied without having to specify #include "test.cpp" in my main.cpp.

However this is not the case, when trying to compile the program I receive multiple errors of cout and the class name "Enemy" not being defined in the test.cpp file.

I tried adding "test.cpp" to main.cpp, but the error did not resolve itself.

Attached are the relevant files.

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

int main(void)
{
	Enemy peon(100,8);
	peon.Attack();
	peon.Defend();
	return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//test.h Class Declaration
#include <iostream>
using namespace std;

class Enemy
{
private:
	int m_hp;
	int m_dmg;
public:
	Enemy(int hp, int dmg);
	~Enemy();
	void Attack();
	void Defend();
	
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//test.cpp Class Definition
Enemy::Enemy(int hp, int dmg):
m_hp(hp),
m_dmg(dmg)
{
	cout << "Enemy Created!\n";
};

Enemy::~Enemy()
{};

Enemy::Attack()
{
	cout << "Peon attacks you for: " << m_dmg << " damage points!";
}

Enemy::Defend()
{
	cout << "Peon raises his shield and blocks your attacks!\n";
	cout << "Peon's hp is only reduced by 10 points!\n";
	m_hp=m_hp-10;
	cout << "Peon's current hp is: " << m_hp << " health points!\n";
}


1
2
3
4
Error	14	error C2264: 'Enemy::Attack' : error in function definition or declaration; function not called	d:\documents\code\c++\scrapbook\scrap1\scrap1\main.cpp	10
Error	10	error C2371: 'Enemy::Attack' : redefinition; different basic types	d:\documents\code\c++\scrapbook\scrap1\scrap1\test.cpp	13
Error	3	error C2550: 'Enemy' : constructor initializer lists are only allowed on constructor definitions	d:\documents\code\c++\scrapbook\scrap1\scrap1\test.cpp	5
Error	1	error C2653: 'Enemy' : is not a class or namespace name	d:\documents\code\c++\scrapbook\scrap1\scrap1\test.cpp	2

Add your "test.cpp" to the "sources" folder of your Visual Studio solution. It is NOT enough to be in the sama folder as main.cpp to be automatically compiled, it must be included in solution.
Don't #include a .cpp file. Like modoran said: add it to the Source Files in your solution explorer.

If you want to play safe, go to your solution explorer, right-click the project and go to Add->Class. In a few clicks in the Wizard, you're certain your files are in the correct place!

[edit]

By the way: test.cpp needs to include test.h! Again, something that the Wizard would've done for you!
Last edited on
Also as a general idea (and to avoid redefinition) use a syntax like this in your header file:
1
2
3
4
5
#ifndef ENEMY_H
#define ENEMY_H
...
//your code here
#endif 


This way you are guaranteed not to have any redefinition problem. This problems tend to increase with the number of source and header files of your program.
Last edited on
Thanks everyone for your help.
I was able to resolve the issue by adding #include "test.h" at the top of my test.cpp file.
I had test.cpp already in the Source Files folder to begin with.

A few more questions:

So for future reference all "classname".cpp files must include "classname".h via an include line?
Also, in regards to eypros suggestion, what that translates to layman terms wise is
if ENEMY_H is not defined
then define ENEMY_H as
//the following code
up until the point of the #ENDIF statement?

the name of the headerfile is test.h not enemy.h, should the declaration match the header file name?
Last edited on
D4rkZ3r0 wrote:
So for future reference all "classname".cpp files must include "classname".h via an include line?

yes

D4rkZ3r0 wrote:
in regards to eypros suggestion, what that translates to layman terms wise is
if ENEMY_H is not defined
then define ENEMY_H as
//the following code
up until the point of the #ENDIF statement?

#ifndef ENEMY_H will do so that the code between that line and #endif will be compiled if ENEMY_H is not defined. #define ENEMY_H just defines ENEMY_H with no particular value. Next time the file is included ENEMY_H is define so #ifndef ENEMY_H will just skip to the #endif and you avoid multiple definition errors.

D4rkZ3r0 wrote:
the name of the headerfile is test.h not enemy.h, should the declaration match the header file name?

It is important that all include guards are unique so it is a good convention to name it after the file name.
Last edited on
Appreciate the insight!
Topic archived. No new replies allowed.