Error with my overloaded operator function?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
using namespace std;

class square{
public:
	square();
friend const square operator <<(ostream& os, const square& square2);

private:
	int height;
};

ostream& operator <<(ostream& os, const square& square2){
	os<<"Height " <<square2.height<<endl;
	os <<endl;
	return os;
}

Keep getting these errors
1
2
3
error C2556:'std::ostream &operator <<(std::ostream &,const RectanglePyramid &)' : overloaded function differs only by return type from 'const RectanglePyramid operator <<(std::ostream &,const RectanglePyramid &)'

error C2373 'operator <<' redefintion; different type modifers


If anyone has any ideas that would be great. I have spent an hour trying to figure out why, first I thought it could be the const after the friend so I took it out and got other errors but these ones disappeared
Last edited on
Try reading the error ;P it tells you exactly what's wrong:

your compiler wrote:
overloaded function differs only by return type


Notice how in your square class you're returning a const square.
But in your function definition you're returning a ostream&

They don't match.
Thank you there were actually two errors but I figured it out thanks!
Last edited on
Topic archived. No new replies allowed.