#pragma once
#include<iostream>
using std::ostream;
template<class T>
class singleList
{
private:
node<T> * head;
public:
//.....
// display entire list
friend ostream& operator<<( ostream& os, const singleList& L )
{
if(L.head)
{
node<T> *iter = L.head;
do
{
os << iter->data << " ";
iter = iter->next;
}while( iter );
}
else
os << "List is empty.";
return os;
}// end of operator<<()
};
Everything is groovy ( it works ).
If I put only a prototype for this in the class definition then define it below ( in the same way I do with all the other class functions ):
#pragma once
#include<iostream>
using std::ostream;
template<class T>
class singleList
{
private:
node<T> * head;
public:
//.....
friend ostream& operator<<( ostream& os, const singleList& L );
};
template<class T>
ostream& operator<<( ostream& os, const singleList<T>& L )
{
if(L.head)
{
node<T> *iter = L.head;
do
{
os << iter->data;
iter = iter->next;
}while( iter );
}
else
os << "List is empty.";
return os;
}
I get the following link error: myLinkLists.obj : 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 singleList<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$singleList@H@@@Z) referenced in function _main
EDIT: In case it's relevant, here is what's in main()
1 2 3 4 5 6 7 8 9 10
int main(void)
{
singleList<int> L;
L.push(1);
// other elements are pushed
cout << L; << endl// error triggered here
return 0;
}
What am I missing? Is there something special about operator overloading in a template class?
EDIT: To correct topic heading