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.
//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!
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.
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?
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.