0

Having read many links about setting up templates on classes I have got to this perplexing compiler error:

Linking...
main.obj : error LNK2019: unresolved external symbol "public: __thiscall test<int,int>::test<int,int>(int)" (??0?$test@HH@@QAE@H@Z) referenced in function _main
fatal error LNK1120: 1 unresolved externals

The offending code, as simply as possible is:

test.h

template<typename U, typename V>
class test {
public:
    test(int number);
};

test.cpp

#include "test.h"

template<typename T, typename U>
test<T, U>::test(int number){}

main.cpp

#include "test.h"

void main() {
    test<int, int> a = test<int, int>(4);
}

Clearly the previous code does nothing useful, I am simply building a model of templates to start a project. Can anyone explain what I'm not understanding about structuring this solution to meet the ends of having a templated class that can construct itself correctly?

J Collins
  • 2,106
  • 1
  • 23
  • 30
  • This should be a duplication - put the template definition in the header –  Oct 15 '13 at 16:45

3 Answers3

0

move the implementation to the header.

RonenKr
  • 213
  • 1
  • 7
0

Because of the way templates work, your implementation must be in the header file, not in a separate .cpp file. So your need this:

template<typename U, typename V>
class test {
public:
    test(int number)
    {
    }
};
Gerald
  • 23,011
  • 10
  • 73
  • 102
0

Besides moving implementation to the header you can also use trick shown in the question at the link template definition and declaration in separate files

Community
  • 1
  • 1
Igor Popov
  • 2,588
  • 17
  • 20