Hello,
I've got three files, main.cpp, test.h (the header file), and test.cpp, (implementation of test.h).
Both main.cpp and test.cpp #include test.h, but I don't seem to be able to get test.h to use the implementation in test.cpp (leading to "undefined reference" errors when attempting to compile).
Searching Google, I've found that g++ should automatically link test.cpp to test.h, but that does not appear to be the case here...
#Including test.cpp (in main.cpp) instead of test.h worked, but Google tells me this is not an especially good idea.
The contents of main.cpp:
#include <iostream>
#include "test.h"
using namespace std;
int main() {
Test MyClass;
MyClass.say_hi();
return 0;
}
The contents of test.h:
#ifndef TEST_H
#define TEST_H
class Test {
public:
void say_hi();
};
#endif
The contents of test.cpp:
#ifndef TEST_CPP
#define TEST_CPP
#include <iostream>
using namespace std;
void Test::say_hi() {
cout << "Hello, world\n";
}
#endif
G++'s output:
$ g++ -Wall -o test main.cpp
/tmp/cc9Lh1zk.o: In function `main':main.cpp:(.text+0x23): undefined reference to `Test::say_hi()'
collect2: ld returned 1 exit status
Thanks for your help...