Template question

I am writing code to work with a driver that is provided. I am relatively new to the idea of templates and am not sure what I am doing wrong here. I am getting the error LNK1120: 1 unresolved externals, and another error LNK2019: unresolved external symbol "public: __thiscall Pair<char>::Pair<char>(char const &,char const &)" (??0?$Pair@D@@QAE@ABD0@Z) referenced in function _main any help would be appreciated.

Driver code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include "pair.h"
using namespace std;


int main()
{
	Pair<char> letters('a', 'd');
	cout << "\nThe first letter is: " << letters.getFirst();
	cout << "\nThe second letter is: " << letters.getSecond();

	cout << endl;
	system("Pause");
	return 0;
}


Header Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
template <class T>
class Pair
{
private:
	T firstChar;
	T secondChar;
public:
	Pair(const T&, const T&);
	T getFirst();
	T getSecond();
};

template <class T>
T Pair<T>::getFirst ( )
{
	return firstChar;
}

template <class T>
T Pair<T>::getSecond ( )
{
	return secondChar;
}
You don't have implementation for that constructor.
Could you be a little more specific? am I required to add another .cpp for implementation?
You have implementations for getFirst and getSecond, but not for constructor.
How would you implement it? I was only told that I needed to declare the constructor like on line 8 of my header code.
After some research I have figured out how to effectively implementing the constructor.

1
2
3
4
5
6
7
template <class T>
Pair<T>::Pair(const T& first, const T& second) :
	firstChar(first),
	secondChar(second)
{

}
Last edited on
That is the implementation; not "calling". The Pair<char> letters('a', 'd'); calls constructor.

There are some special members. See:
http://msdn.microsoft.com/en-us/library/dn457344.aspx
Thank you for showing me my error, it is now correct to say the right thing.
Topic archived. No new replies allowed.