ERROR LNK2019:unresolved external symbol, c++

Nattfrosten picture Nattfrosten · Feb 12, 2013 · Viewed 25.1k times · Source

I have written a template class, for a map/dictionary data structure, and keep getting this strange error (ERROR LNK2019:unresolved external symbol)

Code:

AssArray.h:

#pragma once   
template <class K,class D>   
class AssArray   
{   
    int _size;   
    int _position;   
    D* _data;   
    K* _key;  

public:    
     AssArray(int);
    ~AssArray(void);

    const D& operator [](K k) const;
    D& operator [](K k);
};

AssArray.cpp:

#include "StdAfx.h"
#include "AssArray.h"

template <class K,class D>
AssArray<K,D>::AssArray(int size)
{
    _size=size;
    _data = new D[size];
    _key = new K[size];
    _position=0;
}

template <class K,class D>
AssArray<K,D>::~AssArray(void)
{
        delete[] _data;
    delete[] _key;
}

template <class K,class D>
const D& AssArray<K,D>::operator [](K k) const
{
    //Get
    for(int i=0;i<_position;i++)
        if(_key[i]==d)
            return _data[i];
    return NULL;
}

template <class K,class D>
D& AssArray<K,D>::operator [](K k)
{
    //Set
    for(int i=0;i<_position;i++)
        if(_key[i]==d)
            return _data[i];

    if(_position<_size-1)
    {
        _key[position]=d;
        _position++;
        return _data[_position];
    }
    else
    {
        //Implement error handling
    }

}

The errors are:
1

"Error  4   error LNK1120: 3 unresolved externals   
C:\Users\*****\Documents\Visual Studio 2010\Projects\OOPLAB4NYARE\Debug
\OOPLAB4NYARE.exe   1   1   OOPLAB4NYARE"

2

Error   1   error LNK2019: unresolved external symbol "public: __thiscall 
AssArray<char *,float>::~AssArray<char *,float>(void)" (??1?$AssArray@PADM@@QAE@XZ)
referenced in function _wmain   C:\Users\*****\Documents\Visual Studio
2010\Projects\OOPLAB4NYARE\OOPLAB4NYARE\OOPLAB4NYARE.obj    OOPLAB4NYARE

3

Error   3   error LNK2019: unresolved external symbol "public: __thiscall
AssArray<char *,float>::AssArray<char *,float>(int)" (??0?$AssArray@PADM@@QAE@H@Z) 
referenced in function _wmain   C:\Users\*****\Documents\Visual Studio 
2010\Projects\OOPLAB4NYARE\OOPLAB4NYARE\OOPLAB4NYARE.obj    OOPLAB4NYARE

4

Error   2   error LNK2019: unresolved external symbol "public: float & 
__thiscall AssArray<char *,float>::operator[](char *)" 
(??A?$AssArray@PADM@@QAEAAMPAD@Z) referenced in function _wmain C:\Users\Jonathan
\Documents\Visual Studio 2010\Projects\OOPLAB4NYARE\OOPLAB4NYARE\OOPLAB4NYARE.obj   
OOPLAB4NYARE

I am using Microsoft visual studio 2010 Ultimate. It's most likely something easy I've just been overlooking.

I have tried to clean->rebuild, created a new project and copy pasted the relevant code, as well as trying to find a solution, but the ones I've seen are pretty varied and vague.

Answer

Andy Prowl picture Andy Prowl · Feb 12, 2013

You cannot put member function definitions for a class template in .cpp files.

The definitions of member functions of a class template must be seen by the compiler at the point those functions are invoked, while processing the translation unit (i.e. .cpp file) that contains that invocation. This allows the compiler to actually generate the code. No instantiation occurs unless those functions are invoked.

Now by putting your function definitions in a .cpp file which contains no invocation of those functions, you are basically:

  1. sealing them into a place where no other .cpp file has a chance to see them;
  2. avoiding any instantiation of those functions in the only .cpp file that can see their definitions.

Therefore, the compiler will not generate any object code for them, and the linker will eventually complain that the corresponding symbols are not found (that's the error you get).

To solve the problem, move the member function definitions of your class template into the same header where the class template definition is (AssArray.h in your case), or in a header which is #included by the translation unit(s) where those functions are invoked (and before the points of invocation).