4

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

unable to match function definition to an existing declaration

Even though the two functions it 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 entire error:

1>c:\users\chris\documents\visual studio 2010\projects\testtest\testtest\cmygrid.cpp(33): error C2244: 'CMyGrid::getxPosition' : unable to match function definition to an existing declaration
1> c:\users\chris\documents\visual studio 2010\projects\testtest\testtest\cmygrid.h(18) : see declaration of 'CMyGrid::getxPosition'
1> definition
1> 'int CMyGrid::getxPosition(T *)'
1> existing declarations
1> 'int CMyGrid::getxPosition(T *)'

I have read several other threads with similar problems, and have gotten as far as changing the error to a linker error which I get if I include the function code for the two getposition functions in the header file alongside the declaration. The linker error is:

1>CBoard.obj : error LNK2019: unresolved external symbol "public: __thiscall CMyGrid::CMyGrid(void)" (??0?$CMyGrid@VCBlock@@@@QAE@XZ) referenced in function "public: __thiscall CBoard::CBoard(void)" (??0CBoard@@QAE@XZ)
1>CBoard.obj : error LNK2019: unresolved external symbol "public: __thiscall CMyGrid::~CMyGrid(void)" (??1?$CMyGrid@VCBlock@@@@QAE@XZ) referenced in function "public: __thiscall CBoard::~CBoard(void)" (??1CBoard@@QAE@XZ

Tas
  • 7,023
  • 3
  • 36
  • 51
ChrisC
  • 892
  • 2
  • 12
  • 33
  • What `template` is doing just after includes in .CPP file? – Ajay Jan 26 '13 at 05:45
  • oh, that, one code too much, that changed the error to the linking again ^^ – ChrisC Jan 26 '13 at 13:44
  • So, it is one step ahead! What linking errors you get? – Ajay Jan 26 '13 at 13:47
  • how do í add code in a comment... n1>CBoard.obj : error LNK2019: unresolved external symbol "public: __thiscall CMyGrid::CMyGrid(void)" (??0?$CMyGrid@VCBlock@@@@QAE@XZ) referenced in function "public: __thiscall CBoard::CBoard(void)" (??0CBoard@@QAE@XZ) 1>CBoard.obj : error LNK2019: unresolved external symbol "public: __thiscall CMyGrid::~CMyGrid(void)" (??1?$CMyGrid@VCBlock@@@@QAE@XZ) referenced in function "public: __thiscall CBoard::~CBoard(void)" (??1CBoard@@QAE@XZ) – ChrisC Jan 26 '13 at 14:55
  • It was very much expected. Templates don't get compiled/linked that way. Read this: http://www.codeproject.com/Articles/257589/An-Idiots-Guide-to-Cplusplus-Templates-Part-1 – Ajay Jan 26 '13 at 16:16
  • 2
    Possible duplicate of [Storing C++ template function definitions in a .CPP file](https://stackoverflow.com/questions/115703/storing-c-template-function-definitions-in-a-cpp-file) – Netwave May 25 '17 at 07:20

1 Answers1

0

The problem is that you can't have the implementation of a template in the .cpp. Try to change your code into the .h

Here you have a big post explaining this.

Storing C++ template function definitions in a .CPP file

Netwave
  • 40,134
  • 6
  • 50
  • 93
  • You can have the implementation in the CPP providing you declare the parameters of the template. – c z Sep 03 '21 at 09:54