LNK2019 unresolved external symbol

New to C++ and have been trying to get class templates to work. Ive been getting the following errors in Visual Studio 2015:

Severity Code Description Project File Line Suppression State
Error LNK2019 unresolved external symbol "public: __thiscall TemplateTest<int>::TemplateTest<int>(int,int)" (??0?$TemplateTest@H@@QAE@HH@Z) referenced in function _main

Error LNK2019 unresolved external symbol "public: int __thiscall TemplateTest<int>::bigger(void)" (?bigger@?$TemplateTest@H@@QAEHXZ) referenced in function _main

Heres my code:

1
2
3
4
5
6
7
8
9
10
#pragma once

template <class T>
class TemplateTest {
public:
	TemplateTest(T x, T y);
	T bigger();
private:
	T data1, data2;
};


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "TemplateTest.h"


template <class T>
TemplateTest<T>::TemplateTest(T x, T y) {
	data1 = x;
	data2 = y;
}

template<class T>
T TemplateTest<T>::bigger() {
	if (data1 > data2)
		return data1;
	else
		return data2;
}


1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include "TemplateTest.h"

using namespace std;

int main() {
	TemplateTest <int> intTest(3, 11);
	cout << intTest.bigger() << endl;

	return 0;
}
Hi,

Is the second snippet in a .cpp file? Templates and their function definitions have to all be in the header file.

Consider using a member initilization list instead of assignment in the constructor.
Topic archived. No new replies allowed.