I am trying to compile a minimal example which instantiates a template class. The example compiles fine when a certain order of declarations is kept, but fails otherwise.
temp.h:
#include <iostream>
template <bool display>
class test {
public:
void sayHi();
};
temp.cpp:
#include "temp.h"
template <bool display>
void test<display>::sayHi () {
if (display) std::cout << "Hi";
}
main.cpp:
#include <iostream>
#include "temp.h"
int main () {
test<true> myobject;
myobject.sayHi();
return 0;
}
This is the standard of how to include classes. In GCC 4.4.6, this fails with the error main.cpp:(.text+0x3a): undefined reference to `test::sayHi()'
However, the example compiles when I do a #include "temp.cpp" instead of #include "temp.h" in main.cpp file, so that the compiler reads the class declaration in temp.h first, then sees the content of temp.cpp and only afterwards the content of main.cpp.
When I use non-template classes, it works fine to include just .h files in main.cpp -- what is going wrong here? Note that the temp.cpp is included in my Makefile, so it definitely should not be forgotten by the compiler. Thanks for any help.