C++ LNK 2019 Error with templates/classes - c++

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.

Related

pointer to function methods within the class not working

I'm trying to to write a code that will call a function that receive a pointer to other function within the same class, and call get_num method from main.
but when doing include to TestClass.h from main, I'm getting linkage errors
class TestClass{
public:
void get_num(int num);
void foo(int num, void(TestClass::*function)(int));
void boo(int num);
};
void TestClass::boo(int num)
{
std::cout << "number: " << num << std::endl;
}
void TestClass::foo(int num, void(TestClass::*function)(int))
{
(this->*function)(num);
}
void TestClass::get_num(int num)
{
foo(num, &TestClass::boo);
}
Following is the main code:
#include "TestClass.h"
int main()
{
TestClass tc1;
tc1.get_num(5);
system("pause");
return 1;
}
The following errors appears:
1>main.cpp
1>TestClass.obj : error LNK2005: "public: void __thiscall TestClass::boo(int)" (?boo#TestClass##QAEXH#Z) already defined in main.obj
1>TestClass.obj : error LNK2005: "public: void __thiscall TestClass::foo(int,void (__thiscall TestClass::*)(int))" (?foo#TestClass##QAEXHP81#AEXH#Z#Z) already defined in main.obj
1>TestClass.obj : error LNK2005: "public: void __thiscall TestClass::get_num(int)" (?get_num#TestClass##QAEXH#Z) already defined in main.obj
1>c:\Proj4.exe : fatal error LNK1169: one or more multiply defined symbols found
Pointer to a method is not the same as a pointer to a simple function. If you only want to be able to accept a pointer to the method of the same class you can rewrite your method like this:
void TestClass::foo(int num, void(TestClass::*function)(int))
{
(this->*function)(num);
}
void TestClass::get_num(int num)
{
foo(num, &TestClass::boo);
}
This allows foo() to accept a pointer to any method in TestClass but not to any method of any other class nor to a simple function. It also calls the received method on the same object (note: this in this->*function).

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.

Getting Linker error while instantiating a template class

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.

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

Multiple class in a list c++ [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can templates only be implemented in the header file?
Main class
int main() {
initCarList();
}
void initCarList() {
List<Car> carList;
Car c1 = Car("Toyota", "Bettle", 5);
carList.add(c1);
Car c2 = Car("Mercedes", "Bettle", 7);
carList.add(c2);
Car c3 = Car("FireTruck", "Large Van", 20);
carList.add(c3);
Car c4 = Car("Puma", "Saloon Car", 10);
carList.add(c4);
}
List class
#include "List.h"
#include <iostream>
using namespace std;
template <typename ItemType>
class List {
private:
ItemType itemList[10];
int size;
public:
List();
void add(ItemType);
void del(int index);
bool isEmpty();
ItemType get(int);
int length();
};
template<typename ItemType>
List<ItemType>::List() {
size = 0;
}
template<typename ItemType>
void List<ItemType>::add(ItemType item) {
if(size < MAX_SIZE) {
itemList[size] = item;
size++;
} else {
cout << typename << " list is full.\n";
}
}
I got errors like these
Error 3 error LNK2019: unresolved external symbol "public: void
__thiscall List::add(class Car)" (?add#?$List#VCar####QAEXVCar###Z) referenced in function "void
__cdecl initCarList(void)" (?initCarList##YAXXZ) C:\Users\USER\Desktop\New
folder\DSA_Assignment\main.obj DSA_Assignment
Did I do anything wrongly in my code? NEED HELP THANKS!
There is a syntax error (cout << typename ) in your code. I don't know how you got the linker error. May be its not being compiled at all.
otherwise its okay http://ideone.com/PGWGZu
Clearly you did as it doesn't work! Flippancy aside, let's take a look at the error message bit by bit:
Error 3 error LNK2019: unresolved external symbol
So this is a linkage error. The linker is trying to put together the units that were individually compiled together, but in this case it can't find an external symbol - usually a function or variable name.
"public: void __thiscall List::add(class Worker)" (?add#?$List#VWorker####QAEXVWorker###Z)
This is the full signature of the function that you're missing. It's name manged unfortunately but with your context knowledge of the code that you're writing, you should be able to tell that it's:
void List::add(Worker)
The next bit ...
referenced in function "void __cdecl initWorkerList(void)" (?initWorkerList##YAXXZ) C:\Users\USER\Desktop\New folder\DSA_Assignment\main.obj DSA_Assignment
... is telling you where the problem is happening, i.e where in the code it's trying to link, there is a reference to the missing function. Again, after demangling it's in:
void initWorkerList()
As you can see, with a bit of graft, you can determine exactly what you've done wrong here. Hope this helps.