I am a bit of a newbie in C++, but I just stumbled on the following.
If I have these files:
myclass.hpp:
class myclass {
public:
myclass();
void barf();
};
mymain.cpp:
#include "myclass.hpp"
int main() {
myclass m;
m.barf();
return 0;
}
And I use this implementation of myclass:
myclassA.cpp:
#include <iostream>
using namespace std;
class myclass { // or include myclass.hpp, it both works fine
public:
myclass();
void barf();
};
myclass::myclass() { } //empty
void myclass::barf() {
cout << "barfing\n";
}
then everything is OK. But if I use this implementation of myclass, which is exactly the same except the members are defined inside the class definition, I get a linking error:
myclassB.cpp:
#include <iostream>
using namespace std;
class myclass {
public:
myclass() { }
void barf() {
cout << "barfing\n";
}
};
The error I get:
$ g++ myclassB.cpp mymain.cpp
/tmp/cc4DTnDl.o: In function `main':
mymain.cpp:(.text+0xd): undefined reference to `myclass::myclass()'
mymain.cpp:(.text+0x16): undefined reference to `myclass::barf()'
collect2: ld returned 1 exit status
Apparently the object file built from myclassB.cpp doesn't export the member functions. Why aren't both of these implementations behaving te same? Is there some gotcha rule in C++ that says member definitions inside class definitions are not globally visible, but they are if they are defined outside of the class?