Take the 2-minute tour ×

This question already has an answer here:

When I write C++ code for a class using templates and split the code between a source (CPP) file and a header (H) file, I get a whole lot of "unresolved external symbol" errors when it comes to linking the final executible, despite the object file being correctly built and included in the linking. What's happening here, and how can I fix it?

share|improve this question

marked as duplicate by Deduplicator, Andrew Medico, Kerrek SB, awesomeyi, jaypal singh May 12 '14 at 1:58


See also stackoverflow.com/questions/495021/… –  sth Sep 7 '13 at 1:41

3 Answers 3

up vote 51 down vote accepted

Templated classes and functions are not instantiated until they are used, typically in a separate .cpp file (e.g. the program source). When the template is used, the compiler needs the full code for that function to be able to build the correct function with the appropriate type. However, in this case the code for that function is detailed in the template's source file and hence unavailable.

As a result of all this the compiler just assumes that it's defined elsewhere and only inserts the call to the templated function. When it comes to compile the template's source file, the specific template type that is being used in the program source isn't used there so it still won't generate the code required for the function. This results in the unresolved external symbol.

The solutions available for this are to:

  1. include the full definition of the member function in the template's header file and not have a source file for the template,
  2. define all the member functions in the template's source file as "inline", or
  3. define the member functions in the template's source with the "export" keyword. Unfortunately this isn't supported by a lot of compilers. (Update: this has been removed from the standard as of C++11.)

Both 1 and 2 basically address the problem by giving the compiler access to the full code for the templated function when it is attempting to build the typed function in the program source.

share|improve this answer
In (3) you have an typing error. You probably meant keyword and not keyboard. I don't see how defining the functions as 'inline' will help. You need to put them in the header or explicitly instantiate the templates using the types you need. –  nimrodm Jan 19 '09 at 6:53
you should possibly rephrase (2). no idea what you mean by it –  Johannes Schaub - litb Jan 19 '09 at 7:23
The "export" keyword provides the full definition as well. It may be in some slightly encoded form like a compiler parse tree but it isn't very well hidden. Of course I suppose machine code doesn't hide the source very well either. –  Zan Lynx Jan 19 '09 at 23:26
I have the same problem, and this doesn't answer it... I don't know why this has been accepted as answer. Including the full definition of the member functions works, but, in my opinion, it represents a lack of security to our programs, and generally a bad practice that will lead to unorganized code. –  Victor Mar 17 '14 at 8:30
I'm also confused by (2). And there seems to it since 2009 (wow, by that time I didn't know what templates are). And, putting the function into .h file does not help. –  Tomáš Zato Jun 18 '14 at 23:53

Another option is to put the code in the cpp file and in the same cpp file add explicit instantiations of the template with the types you expect to be using. This is useful if you know you're only going to be using it for a couple of types you know in advance.

share|improve this answer

For each file that include the .h file you should be to insert both lines:

#include "MyfileWithTemplatesDeclaration.h"
#include "MyfileWithTemplatesDefinition.cpp"


#include "list.h"
    #include "list.cpp" //<---for to fix bug link err 2019

    int main(int argc, _TCHAR* argv[])
        list<int> my_list;

also, you dont forget to place your declaration class among centinel constants

#ifndef LIST_H
#define LIST_H
#include <iostream>
template <class T>
class list
    int m_size,
    T m_line;
    node<T> *m_head;
    void add_end(T);
    void print();
share|improve this answer
I don't think this is a great idea. Including .cpp files sends the wrong message. If you intend the user to include both files. name them code.h and code_impl.h or similar. –  Mark McKenna Mar 8 '13 at 15:21
I agree. There is little reason to ever have to include a .cpp file in your source, and depending on your project settings this might even give the compiler a separate headache –  RectangleEquals Mar 12 '13 at 6:31
