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':|testClass::testClass()’|
*voip*\test\main.cpp
|6|undefined reference to
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)
Code::Blocks doesn’t know that it has to compile
test.cppand produce an object filetest.o(so that the latter may be linked together withmain.oto produce the executable). You have to addtest.cppto your project.In Code::Blocks, go to
Project>Add Filein the menu and select yourtest.cppfile. 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 settingsand selectFull command linein theCompiler loggingdrop 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.