Templates in external files doesn't work well

It works but when i try to move class and methods to separate files it throws errors
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  #include <iostream>

using namespace std;

template<typename T>
class Test
{
    public:
        Test();
        virtual ~Test();

    protected:

    private:
        T* val;
};

template<typename T>
Test<T>::Test()
{
    //ctor
}
template<typename T>
Test<T>::~Test()
{
    //dtor
}

int main()
{
    Test<int> test;

    cout << "Hello world!" << endl;
    return 0;
}


So i tried to

Test.h <- file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef TEST_H
#define TEST_H

template<typename T>
class Test
{
    public:
        Test();
        virtual ~Test();

    protected:

    private:
        T* val;
};

#endif // TEST_H


Test.cpp <- file
1
2
3
4
5
6
7
8
9
10
11
#include "Test.h"
template<typename T>
Test<T>::Test()
{
    //ctor
}
template<typename T>
Test<T>::~Test()
{
    //dtor
}


then include in main it throws errors
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <Test.h>

using namespace std;

int main()
{
    Test<int> test;

    cout << "Hello world!" << endl;
    return 0;
}

it throws
undefined reference to `Test<int>::Test()
undefined reference to `Test<int>::~Test()
undefined reference to `Test<int>::~Test()

what goes wrong?
Last edited on
Topic archived. No new replies allowed.