views:

129

answers:

5

So I'm trying to learn C++ and I've gotten as far as using header files. They really make no sense to me. I've tried many combinations of this but nothing so far has worked:

Main.cpp:

#include "test.h"

int main() {
    testClass Player1;
    return 0;
}

test.h:

#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED
class testClass {
    private:
        int health;
    public:
        testClass();
        ~testClass();
        int getHealth();
        void setHealth(int inH);
};
#endif // TEST_H_INCLUDED

test.cpp:

#include "test.h"

testClass::testClass() { health = 100; }
testClass::~testClass() {}

int testClass::getHealth() { return(health); }
void testClass::setHealth(int inH) { health = inH; }

What I'm trying to do is pretty simple, but the way the header files work just makes no sense to me at all. Code blocks returns the following on build:

obj\Debug\main.o(.text+0x131)||In function main':| *voip*\test\main.cpp |6|undefined reference totestClass::testClass()'| obj\Debug\main.o(.text+0x13c):voip\test\main.cpp|7|undefined reference to `testClass::~testClass()'| ||=== Build finished: 2 errors, 0 warnings ===|

I'd appreciate any help. Or if you have a decent tutorial for it, that would be fine too (most of the tutorials I've googled haven't helped)

+3  A: 

There's nothing wrong with the way you've set up the header. Your error is occurring during linking. What's your gcc command line? My guess is that you're compiling just main.cpp, and forgot test.cpp.

I'm not sure. I was just clicking "build and run". I thought that would compile all files and link them automatically. I'm not terribly familiar with Codeblocks or any of this yet, but I think you're both right. I've tried building it by itself and it returns that it has no target.
Karl
A: 

What command(s) are you using to build? It seems that you are not compiling and linking in test.cpp, so when main.cpp goes to look for the appropriate symbols, it cannot find them (link failure).

VeeArr
A: 

as said in the other answers, this is a link error. compile and link like this:

g++ Main.cpp test.cpp -o myprogram -Wall -Werror
Aaron
The OP is using an IDE, not the command line.
Emile Cormier
A: 

Some (brief) information about header files too -- the #include line in your .cpp files just instructs the compiler to paste in the contents of that file into the stream-to-be-compiled at that point. So they let you declare the testClass in one place (test.h) and use it in many places. (main.cpp, someother.cpp, blah.cpp). Your test.cpp contains the definitions of methods of testClass so you need to link it into the final executable as well.

But there's nothing magic about header files, it's just simple text substitution used for convenience so that you don't have to declare the same class or functions over and over again. You've (correctly) got that #ifndef TEST_H_INCLUDED stuff in there so that in the chance you have someother.h which #includes test.h and main.cpp #includes both test.h and someother.h, you'll only get a single copy of the testClass declaration.

Hope this helps!

Mike Kale
Thanks man, I was happy to hear that I got the header bit right. Probably just me not knowing how to use the IDE.
Karl
+2  A: 

Code::Blocks doesn't know that it has to compile test.cpp and produce an object file test.o (so that the latter may be linked together with main.o to produce the executable). You have to add test.cpp to your project.

In Code::Blocks, go to Project>Add File in the menu and select your test.cpp file. Make sure that both Release and Debug checkboxes are checked.

Then Build->Rebuild.

EDIT:

Here's a tip to help you see what the IDE is doing under the hood when compiling. Go to Settings -> Compiler and Debugger -> Global Compiler Settings -> Other settings and select Full command line in the Compiler logging drop box. Now, whenever you build, the gcc compiler commands will be logged in the Build Log. Whenever someone on StackOverflow asks you for the gcc command line you used, you can copy and paste what's in the Build Log.

Emile Cormier
Thanks man, that solved it. Thanks for the tips too.
Karl