overloading operator<< in a template class

closed account (D80DSL3A)
If I define the << overload within the class definition like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#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 ):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#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
Last edited on
There is something special about friend functions of a template class. Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#pragma once
#include<iostream>
using std::ostream;

template <class T>
class singleList;

template<class T>
ostream& operator<< ( ostream& os, const singleList<T>& L );

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;
}
Last edited on
closed account (D80DSL3A)
Thank you m4ster r0shi. That works great!

@ne555 Thanks for the link. I'll read up on this so I understand the issue better.
Last edited on
Topic archived. No new replies allowed.