So I'm working on a linked list class, for a class I'm taking. I've basically just started working on it and I'm getting an unknown error that I really don't know how to deal with. The compiler is not returning an error in my code really, its returning a bunch of mess. Here is the first line of the error.

/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/ios_base.h: In copy constructor âstd::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)â:

I've had something like this before and was able to fix whatever the issue is but now, I can't figure out what I'm doing wrong.

So I have a class called LinkedList, no big deal. Here is what I'm doing.

ostream operator<<(ostream &out, LinkedList list);

Now I add the actual definition to my implementation file and it looks like this:

void LinkedList::display(ostream &out) { 
	NodePointer cur; 
	while(cur != NULL) { 
		out << cur->data << endl; 
		cur = cur->next; 
	} 
}
ostream operator<<(ostream &out, LinkedList list) {
	list.display(out); 
	return out;

Note that NodePointer is a typedef of Node *, and Node is a sub class of my LinkedList class.

Now my error comes from the line return out and I know this because the program will compile with this line.

Help on this would be much appreciated!

Return a reference to the stream object, that's the way it has been designed to be done (the compiler complains about the streams copy constructor), so

ostream & operator<<(ostream &out, LinkedList list) {
	list.display(out); 
	return out;

Then furthermore, basically you want to pass class/struct objects by reference (or even by const reference) to avoid temporary objects being created upon function calls, so maybe change to

ostream & operator<<(ostream &out, LinkedList & list) {
	list.display(out); 
	return out;
This article has been dead for over six months. Start a new discussion instead.