3

Possible Duplicate:
Why do I get “unresolved external symbol” errors when using templates?

I am using templates in my code while there is always an error LNK2019. Here is part of my code:

Method.h

template<typename type>
void Method(Model<type>* sys);

Method.cpp

template<typename type>
void Method(Model<type>* sys){ blablabla;}

Model.h

template<typename type>
class Model{ blablabla;}

class Model1:public Model<double>{ blablabla;}

Main.cpp

Model<double> *sys=new Model1();
Method(sys);

However, there always shows an error LNK2019: unresolved external symbol "void __cdec1 Method(class Model*)" referenced in function_main. Anyone knows where I am going wrong? Thanks a lot!

Community
  • 1
  • 1
Mark Z.
  • 655
  • 2
  • 9
  • 12
  • 1
    Put implementations in the header... – Luchian Grigore Jul 16 '12 at 21:17
  • Most of the questions here with this "issue" are named like this: "unresolved external symbol bla-bla-bla templates".. And all of them will come up as suggestions, while you're typing the title of your question. Just pay attention! – Kiril Kirov Jul 16 '12 at 21:33

3 Answers3

20

Templates should be implemented in the header.

Method.h

template<typename type>
void Method(Model<type>* sys){ /*Your code here*/ };

See also: https://stackoverflow.com/a/495056/868546

From Mark Ransom:

In the .cpp file, the compiler can't guess what the template parameter will be when you use the function in other files, so it doesn't generate any actual code. The linker notices the lack of code and complains.

The usual way is to define the entire function body in the header, much as an inline function, so the compiler can emit the code when it sees the function being used with the template parameter filled in.

Community
  • 1
  • 1
Drise
  • 4,310
  • 5
  • 41
  • 66
  • It's not strictly necessary for the template to be implemented in the header, as long as one source file somewhere provides an implementation using the template parameter that you'll be using elsewhere in the code. As a practical matter though, this is the way everybody does it because it always just works. – Mark Ransom Jul 17 '12 at 17:12
  • instead of implementing it in the header file, include the .cpp file instead of .h file, its much simpler and solves the problem – revolutionary Oct 06 '14 at 10:07
  • It is of my opinion that anything that *defines* things should be in the header, and anything that actually does stuff (i.e. implemented functions, etc) should be in the cpp file. This keeps code clean and easily navigable by other readers. – Drise Jan 13 '15 at 03:57
8

In the .cpp file, the compiler can't guess what the template parameter will be when you use the function in other files, so it doesn't generate any actual code. The linker notices the lack of code and complains.

The usual way is to define the entire function body in the header, much as an inline function, so the compiler can emit the code when it sees the function being used with the template parameter filled in.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Mark, I'd like to include what you said into my answer. It gives a very simple explanation as to why this occurs, which could help any future visitors. – Drise Jul 17 '12 at 17:05
  • @Drise, thanks for asking. StackOverflow encourages this kind of answer amalgamation, and as a long-time StackOverflow booster I'm all in favor. Go right ahead. – Mark Ransom Jul 17 '12 at 17:09
  • You're welcome. And thank you for allowing me to do so. – Drise Jul 17 '12 at 17:17
0

Templates should be implemented in a header or you can use explicit instantiation of a template function/class.

qehgt
  • 2,972
  • 1
  • 22
  • 36