Hi,
after a lot of years of C-Sharp development I moved back to the good old c++ now.
One new feature that is new to me is templates and I run into some trouble with it as soon as I try to implement the functions of a templated class in a seperate file.
Working:
test.h:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#ifndef TEST_H
#define TEST_H
namespace neitzel
{
template <typename T> class Test
{
public:
Test();
void Print();
};
}
#endif // TEST_H
| |
main.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include <iostream>
#include "test.h"
using namespace std;
using namespace neitzel;
int main(int argc, char **argv)
{
Test<int> *test = new Test<int>();
test->Print();
return 0;
}
namespace neitzel
{
template <typename T> Test<T>::Test()
{
}
template <typename T> void Test<T>::Print()
{
cout << "Hello World!" << endl;
}
}
| |
g++ main.cpp
./a.out
and I get my Hello world.
Now I remove the namespace neitzel { ... } part from main.cpp and simply put it inside test.cpp:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
|
#include <iostream>
#include "test.h"
using namespace std;
namespace neitzel
{
template <typename T> Test<T>::Test()
{
}
template <typename T> void Test<T>::Print()
{
cout << "Hello World!" << endl;
}
}
| |
and I try to compile it with
g++ main.cpp test.cpp
/tmp/ccEc8cEz.o: In function `main':
main.cpp:(.text+0x1d): undefined reference to `neitzel::Test<int>::Test()'
main.cpp:(.text+0x2d): undefined reference to `neitzel::Test<int>::Print()'
collect2: error: ld returned 1 exit status
I can remove the template stuff from the code and the compile works fine with the separation into multiple files ...
I think I am missing something easy and stupid - it would be great if you could help me on this issue.
With kind regards,
Konrad