I get this error:

error C2785: 'std::ostream &operator <<(std::ostream &,const Outsider<T>::Nested &)' and '<Unknown>' have different return types

When trying to compile the following code with VS2008:

#include <iostream>

using namespace std;

template <class T>
class Outsider {
	private:
		class Nested {
			private:
				T myData;
			public:
				Nested(const T &);
				friend ostream & operator << <>(ostream &, const Nested &);
		};
		Nested *myNested;
	public:
		Outsider(const T &);
		~Outsider();
		friend ostream & operator << <>(ostream &, const Outsider &);
};

template <class T>
Outsider<T>::Nested::Nested(const T &data) : myData(data) {}

template <class T>
ostream & operator << <>(ostream &out, const typename Outsider<T>::Nested &nested) {
	return out << data << endl;
}

template <class T>
Outsider<T>::Outsider(const T &data) : myNested(new Nested(data)) {}

template <class T>
Outsider<T>::~Outsider() {
	delete myNested;
}

template <class T>
ostream & operator << <>(ostream &out, const Outsider<T> &outsider) {
	return out << *outsider.myNested << endl;
}

void main() {
	Outsider<int> outsider(5);
	cout << outsider << endl;
}

Operator << compiles cleanly for the Outsider class but not for the Nested class. Why is this? How can I fix it?

Thanks

what is that <> ?

This compiled ok for me. all I did was removed the <> friend ostream & operator << (ostream &, const Nested &);

Thanks for answering.

What compiler are you using? I still can't compile even thou I did what you suggested:

I tried removing the <> in the declarations as well as the definitions and I still can't compile.

If I remove all <> (both in declarations and in definitions) I get an unresolved external symbol linker error for the operator << of Outsider.

If I leave the <> just in the definitions I get the same error.

And if I leave the <> just in the declarations or I use it both in declarations and in definitions, I get the original error:

error C2785: 'std::ostream &operator <<(std::ostream &,const Outsider<T>::Nested &)' and '<Unknown>' have different return types

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.