Getting Linker error while instantiating a template class - c++

While instantiating the class in main I am getting these two errors:
error LNK2019: unresolved external symbol "public: __thiscall templateClass<int>::templateClass<int>(void)" (??0?$templateClass#H##QAE#XZ) referenced in function _wmain
error LNK2019: unresolved external symbol "public: __thiscall templateClass<int>::~templateClass<int>(void)" (??1?$templateClass#H##QAE#XZ) referenced in function _wmain
Code
templateClass.h
#pragma once
#define MAX 10
template<class T>
class templateClass
{
private:
T arr[MAX];
int size;
public:
templateClass(void);
void initArrayWithZero();
void pushElementIntoArray(T element );
void printArray();
~templateClass(void);
};
templateclass.c
#include "templateClass.h"
template <class T>
templateClass<T>::templateClass(void)
{
size = 0;
}
template <class T>
templateClass<T>::~templateClass(void)
{
}
template <class T>
void templateClass<T>::pushElementIntoArray(T element )
{
if(size<MAX)
{
T[size++] = element;
}
else
{
//Give error code
}
}
template <class T>
void templateClass<T>::initArrayWithZero()
{
for(i= 0; i<MAX; i++)
T[i] = 0;
}
template <class T>
void templateClass<T>::printArray()
{
for( int i =0; i<MAX; i++)
cout<<"\n"T[i];
}
Main.cpp
#include "stdafx.h"
#include "templateClass.h"
int _tmain(int argc, _TCHAR* argv[])
{
templateClass<int> intarray;
return 0;
}
How can I fix this?

Template classes such as templateClass<T> must (usually) be implemented in header (.h) files.
As such, you need to move the content of templateclass.c to templateClass.h and delete templateclass.c.
See Why can templates only be implemented in the header file? for more information.

Related

LNK2019: Unresolved external symbol error - with member function of class template

I am trying to create a custom List in c++.
I've defined it this way:
List.h:
#include "ListItem.h"
#pragma once
template<class T> class List
{
private:
ListItem<T>* first;
public:
T* GetAt(int);
ListItem<T>* GetLastListItem();
void Add(T*);
void Clear();
};
List.cpp:
#include "stdafx.h"
#include "List.h"
template<class T> T* List<T>::GetAt(int index)
{
if (!first)
return 0;
ListItem<T>* current = first;
for (int i = 1; i < index; i++)
{
current = current->GetNext();
}
return current->GetItem();
}
template<class T> L...
main:
List<TestItem> liste;
TestItem ti; //just a int inside.
liste.Add(&ti);
I am getting the following errors:
1>ConsoleApplication1.obj : error LNK2019: unresolved external symbol ""public: void __thiscall List::Add(class TestItem *)" (?Add#?$List#VTestItem####QAEXPAVTestItem###Z)" in function"_main".

how do you pass an array to a template function? [duplicate]

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
Closed 8 years ago.
In my main I create an object from a template class, then I call my sort method (template method)
However it get a error in my main.obj file.
error:
LNK2019: unresolved external symbol "public: void __thiscall SelectionSort<int [0]>::IterativeSort(int * const,unsigned int)" ............
Call in main:
SelectionSort<int[]> smallArraySort;
smallArraySort.IterativeSort(smallArray, smallSize);
Header file: SelectionSort.h
template <class T>
class SelectionSort
{
public:
void IterativeSort(T data, unsigned int size);
void RecursiveSort(T data, unsigned int size);
};
sort code: SelectionSort.cpp
#include "SelectionSort.h"
template<class T>
void SelectionSort<T> ::IterativeSort(T data, unsigned int size)
{
int temp = data[0];
int greaterNum = 0
for (unsigned int index = 0; index < size; index++)
{
for (unsigned int inner = index; inner < size; inner++)
{
if (temp>data[inner])
{
greaterNum = temp;
temp = data[inner];
data[inner] = greaterNum;
}
}
}
}
This happens because you implement the methods of the template in a .cpp file where they cannot be seen when they need to be instantiated. In general, templates must be defined in a header file.
To solve the problem, eliminate your SelectionSort.cpp file by moving its definitions into SelectionSort.h.

C++ LNK 2019 Error with templates/classes

So this part of the code is a small part of a large project. I can't copy the entire project but it's really just 2 lines making the error. I'm trying to access the function "mergeSort" from the class Metrics. Metrics is a different class than the one I'm trying to access it in. I've defined mergeSort and also #included Metrics in the class I'm working with. I've also defined the constructor. Here is the piece of code:
void Document::createHashWords()
{
Node* heads[97];
string* allWords = parseWords();
int numWords = getdWordCt();
for ( int i = 0; i < numWords; i++ )
{
char* word = (char*)allWords[i].c_str();
int k = hashWord(word,97);
insertWord(heads,k,allWords[i]);
}
for ( int i = 0; i < 97; i++ )
{
Node* temp = heads[i];
int size = 0;
while (temp != NULL)
{
size++;
temp = temp->getNext();
}
int* countArr = new int[size];
temp = heads[i];
for ( int i = 0; i < size; i++ )
{
countArr[i] = temp->getCount();
temp = temp->getNext();
}
Metrics <int> toSort;
toSort.mergeSort(countArr,size);
}
}
the Code that is causing the problem is the last 2: Metrics toSort and toSort.mergeSort(countArr,size);.
Here is the Metrics class:
template <class T>
class Metrics
{
private:
int id;
public:
Metrics();
~Metrics();
void selectionSort(T *arr, int n);
void insertionSort(T *arr, int n);
void merge(T *a, int numA, T *b, int numB, T *c);
void mergeSort(T *arr, int n);
};
I've defined mergeSort in the .cpp file for that class.
Here is my build error:
1>Document.obj : error LNK2019: unresolved external symbol "public: __thiscall Metrics<int>::Metrics<int>(void)" (??0?$Metrics#H##QAE#XZ) referenced in function "public: void __thiscall Document::createHashWords(void)" (?createHashWords#Document##QAEXXZ)
1>Document.obj : error LNK2019: unresolved external symbol "public: __thiscall Metrics<int>::~Metrics<int>(void)" (??1?$Metrics#H##QAE#XZ) referenced in function "public: void __thiscall Document::createHashWords(void)" (?createHashWords#Document##QAEXXZ)
1>Document.obj : error LNK2019: unresolved external symbol "public: void __thiscall Metrics<int>::mergeSort(int *,int)" (?mergeSort#?$Metrics#H##QAEXPAHH#Z) referenced in function "public: void __thiscall Document::createHashWords(void)" (?createHashWords#Document##QAEXXZ)
1>C:\Users\Kevin\Documents\Visual Studio 2012\Projects\CMPSC 122 Checkpoint 2\Debug\CMPSC 122 Checkpoint 1.exe : fatal error LNK1120: 3 unresolved externals
If I remove those 2 lines, I can compile fine. I just don't know why those 2 lines are wrong. I need to access that function from the Metrics class.
template <class T>
class Metrics
{
private:
int id;
public:
Metrics<T>() {}
~Metrics<T>() {}
void selectionSort(T *arr, int n);
void insertionSort(T *arr, int n);
void merge(T *a, int numA, T *b, int numB, T *c);
void mergeSort(T *arr, int n);
};
So this is now my Metrics class:
template <class T>
class Metrics
{
private:
int id;
public:
Metrics() {}
~Metrics() {}
void selectionSort(T *arr, int n);
void insertionSort(T *arr, int n);
void merge(T *a, int numA, T *b, int numB, T *c);
void mergeSort(T *arr, int n);
};
I commented out the definitions of the constructor/destructor in the Metrics.cpp file like so:
//template <class T>
//Metrics<T>::Metrics()
//{
//}
//template <class T>
//Metrics<T>::~Metrics()
//{
//}
And now this is my error code:
1>Document.obj : error LNK2019: unresolved external symbol "public: void __thiscall Metrics::mergeSort(int *,int)" (?mergeSort#?$Metrics#H##QAEXPAHH#Z) referenced in function "public: void __thiscall Document::createHashWords(void)" (?createHashWords#Document##QAEXXZ)
1>C:\Users\Kevin\Documents\Visual Studio 2012\Projects\CMPSC 122 Checkpoint 2\Debug\CMPSC 122 Checkpoint 1.exe : fatal error LNK1120: 1 unresolved externals
The missing symbol seems to be the default ctor for Metrics, the one that takes no arguments. Have you defined it? You need to get it included somehow in the same translation unit. The usual way would be to define it in the header file:
template <typename T>
Metrics<T>::Metrics() {}
Template functions need to be defined in every translation unit (.cpp) they're used in, so you want all of Metrics' member functions to be defined in Metrics.h, as if they were all inlined.
They work differently from normal functions because the code generated is different for every different type used for T, so the compiler can't know what versions to generate when compiling Metrics.cpp.

unable to match function definition to an existing declaration with class template

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
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

C++: Templates not working from another class

When the following project is compiled, I get the following compiler error: (Visual Studio 2010)
1>usingclass.obj : error LNK2019: unresolved external symbol "public: static int __cdecl c1::arrSize(int * const)" (??$arrSize#H#c1##SAHQAH#Z) referenced in function "public: void __thiscall usingclass::a(void)" (?a#usingclass##QAEXXZ)
Code:
Headers:
c1.h
#pragma once
#include <array>
class c1
{
c1(void);
~c1(void);
public:
template<class T>
static int arrSize(T arr[]);
};
usingclass.h
#pragma once
#include "c1.h"
class usingclass
{
public:
usingclass(void);
~usingclass(void);
void a();
};
Source files:
c1.cpp
#include "c1.h"
c1::c1(void)
{
}
c1::~c1(void)
{
}
template <class T>
int c1::arrSize(T arr[])
{
return (sizeof(arr)/sizeof(arr[0]));
}
usingclass.cpp
#include "usingclass.h"
usingclass::usingclass(void)
{
}
usingclass::~usingclass(void)
{
}
void usingclass::a()
{
int a[2] = {1,2};
int b = c1::arrSize<int>(a);
}
How do I fix that?
Don't do this! The declaration is misleading.
template <class T>
int c1::arrSize(T arr[])
{
return (sizeof(arr)/sizeof(arr[0]));
}
is equivalent to
template <class T>
int c1::arrSize(T *arr)
{
return (sizeof(arr)/sizeof(arr[0]));
}
which will not give you want you want. The proper way to do it is like this:
class c1
{
c1(void);
~c1(void);
public:
template<class T,int N>
static int arrSize(T (&arr)[N]) { return N; }
};
arrSize takes a reference to an array as a parameter. The type of the array and the size of the array are template parameters and can be deduced by the compiler. The inline member function then just returns the size determined by the compiler.
You need to move
template <class T>
int c1::arrSize(T arr[])
{
return (sizeof(arr)/sizeof(arr[0]));
}
inside c1.h.
Template implementations must be visible to all translation units using that template (unless it's specialized, and in your case it's not).
This solves the compiler error but the underlying issue is solved with Vaughn Cato's answer. I missed that. You'll still need the definition in the header.
I think you have to define your template in c1.h itself. Because when you are including c1.h in your usingclass.h, and try to use template it does not find the expansion for template.
Or If you want to go with implementation of template in c1.cpp, then you have to include c1.cpp as well in usingclass.h.