Failed to map function definition to existing declaration with class template

I created a template class that should store the grid as a 2-dimensional std::vector ; however, when I compile using VC ++ (2010, if that matters, but I doubt it), I get the following error:

cannot match function definition with existing declaration

Despite the fact that the two functions that he is trying to match are exactly equal.

Here is the code in the header file:

 #pragma once #include "CBlock.h" template<class T> class CMyGrid{ public: long sizeX; long sizeY; /*block position on grid*/ std::vector<std::vector<T*>> System; CMyGrid(); ~CMyGrid(); CMyGrid(int sizeXp, int sizeYp){sizeX = sizeXp; sizeY =sizeYp;}; void Set(T *data, int x, int y){System.at(x).at(y) = data;}; int GetSizeX(){return sizeX;} int GetSizeY(){return sizeY;} int getxPosition(T *data); /*make easier put in struct*/ int getyPosition(T *data); /*size*/ /* will hopefully be sizex by sizey*/ }; 

And here is the cpp file:

 #include "stdafx.h" #include "CMyGrid.h" #include "CBlock.h" template <class T> template <class T> int CMyGrid<T>::getxPosition(T *data) { for (int i = 0; i <System.size(); i++) { for (int j = 0; j < System[i].size(); j++) { if data == System[i][j]; return j; else continue; } } } template <class T> int CMyGrid<T>::getyPosition(T *data) { for (int i = 0; i <System.size(); i++) { for (int j = 0; j < System[i].size(); j++) { if data == System[i][j]; return i; else continue; } } } 

Here's the complete error:

1> c: \ users \ chris \ documents \ visual studio 2010 \ projects \ testtest \ testtest \ cmygrid.cpp (33): error C2244: 'CMyGrid :: getxPosition': it is not possible to match the function definition with an existing declaration
1> c: \ users \ chris \ documents \ visual studio 2010 \ projects \ testtest \ testtest \ cmygrid.h (18): see the declaration "CMyGrid :: getxPosition"
1> definition
1> 'int CMyGrid :: getxPosition (T *)'
1> existing ads
1> 'int CMyGrid :: getxPosition (T *)'

I read several other threads with similar problems and got to changing the error to the linker error that I get if I include the function code for the two getposition functions in the header file along with the declaration. Linker Error:

1> CBoard.obj: error LNK2019: unresolved external character "public: __thiscall CMyGrid :: CMyGrid (void)" (? 0? $ CMyGrid @VCBlock @@@@ QAE @XZ) referenced by the function "public: __thiscall CBoard :: CBoard (void) "(0CBoard @@ QAE @XZ)
1> CBoard.obj: error LNK2019: unresolved external character "public: __thiscall CMyGrid :: ~ CMyGrid (void)" (? 1? $ CMyGrid @VCBlock @@@@ QAE @XZ), referred to by the function "public: __thiscall CBoard :: ~ CBoard (void) "(?? 1CBoard @@ QAE @XZ

+4
source share
1 answer

The problem is that you cannot implement the template in .cpp . Try changing the code to .h

Here you have a great post explaining this.

Saving C ++ Template Function Definitions in a .CPP File

0
source

All Articles