4

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!

2
  • 1
    Put implementations in the header... Commented Jul 16, 2012 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! Commented Jul 16, 2012 at 21:33

3 Answers 3

22

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
instead of implementing it in the header file, include the .cpp file instead of .h file, its much simpler and solves the problem
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.
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.

3 Comments

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, 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.
You're welcome. And thank you for allowing me to do so.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.