I am having a compiler error implementing operator<< on a C++ template class. Here is the code:
#pragma once
#include <iostream>
using namespace std;
//a template class to process a pair of "things"
template <typename T>
class Pair
{
private:
T first;
T second;
public:
Pair();
Pair(T, T);
friend ostream& operator<<(ostream&, Pair); //also tried Pair<T>
};
template <typename T>
Pair<T>::Pair()
{ }
template <typename T>
Pair<T>::Pair(T one, T two)
{
first = one;
second = two;
}
template <typename T>
ostream& operator<<(ostream& out, Pair<T> other)
{
out << "{ " << other.first << " and " << other.second << " }";
return out;
}
//test code
int main(){
Pair<string> words("red", "apple");
cout << "words is " << words << endl;
}
I am getting linker error
LNK2019 unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Pair<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@V?$Pair@V?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@@@Z) referenced in function _main