Overload operator << for a template-nested private class
Apr 30, 2009 at 7:41am UTC
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:
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 38 39 40 41 42 43 44 45 46
#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
Apr 30, 2009 at 8:30am UTC
More experencied programmers will correct me, but template definitions and declarations must be at the same place.
Also:
main() returns an int;
This is totally your decision but I think it's a bad practice that the
ostream & operator <<(ostream &, const someOtherClass&)
flushes the stream, and inserts a new line with <<std::endl.
So this is what I made from your code, this prints "5".
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
#include <iostream>
using namespace std;
template <class T>
class Outsider {
private :
class Nested {
private :
T myData;
public :
Nested(const T &data): myData(data) {}
friend ostream & operator << (ostream &out, const Nested &nested) {
return out << nested.myData << endl;
}
};
Nested *myNested;
public :
Outsider(const T &data) : myNested(new Nested(data)) {}
~Outsider() {
delete myNested;
}
friend ostream & operator << (ostream &out, const Outsider &outsider) {
return out << *outsider.myNested << endl;
}
};
int main() {
Outsider<int > outsider(5);
cout << outsider << endl;
}
R0mai
Last edited on Apr 30, 2009 at 8:30am UTC
Apr 30, 2009 at 12:23pm UTC
The problem is an extra <> in OP's operator<< declarations. Note they are missing in the response, which is the correct way to declare them.
Apr 30, 2009 at 5:49pm UTC
Thanks to both for the responses.
@R0mai:
I don't want to make the functions inline, can you post a code the compiles with function definitions outside of the class?
@jsmith:
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
Apr 30, 2009 at 6:52pm UTC
Just move the implementations of the operator<< into the class at the point of declaration.
Topic archived. No new replies allowed.