Operator Overloading Error

Trying to overload << operator in my this program, (which I think is fine), I'm unable to compile the program, error which I get are written in the end

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
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <ostream>
using namespace std;
class Circle
{
	friend ostream &operator << (ostream&, Circle&);
public:

	Circle (double r=0, int x=0, int y=0);
	void setRadius (double);
	void setPoints (int, int);

	double area () const;
private:
	
	double radius;
	int x,y;
};

Circle::Circle (double r, int a, int b)
{
	x=a;
	y=b;
	radius=r;
}

void Circle::setRadius(double r)
{
	radius=r;	// assigns value of r to radius
}

void Circle::setPoints (int a, int b)
{
	x=a;	// x co ordinate of circle's position
	y=b;	// y co ordinate of circle's position
}

double Circle::area() const
{
	return 3.14159*radius*radius;	// calculate and return radius
}

 ostream& operator << (ostream & output,  Circle & c)
{
	output<<"Radius: "<<c.radius<<endl;
	output<<"Co-Ordinates: ["<<c.x<<","<<c.y<<"]"<<endl;
	return output;
}

////////////////////////////////////////////////////////////////////////////////////////////
int main ()
{
	Circle c(3.47,2,5);
	cout<<c;
	return 0;
}

Compiling...
overloading.cpp
D:\C++ Project\Practice\overloading.cpp(45) : error C2248: 'radius' : cannot access private member declared in class 'Circle'
D:\C++ Project\Practice\overloading.cpp(16) : see declaration of 'radius'
D:\C++ Project\Practice\overloading.cpp(46) : error C2248: 'x' : cannot access private member declared in class 'Circle'
D:\C++ Project\Practice\overloading.cpp(17) : see declaration of 'x'
D:\C++ Project\Practice\overloading.cpp(46) : error C2248: 'y' : cannot access private member declared in class 'Circle'
D:\C++ Project\Practice\overloading.cpp(17) : see declaration of 'y'
D:\C++ Project\Practice\overloading.cpp(54) : error C2593: 'operator <<' is ambiguous
Error executing cl.exe.

overloading.exe - 4 error(s), 0 warning(s)
Last edited on
The above code compiled for me with no errors, and gave the following output:
Radius: 3.47
Co-Ordinates: [2,5]


Which compiler are you using?
Last edited on
I does not compile to me.. I'm using visual C++ 6.0
Compiles fine for me too. Visual C++ is crap(it's really behind, not mentioning your version), switch to clang or gcc(package is called MinGW on Windows, I believe), and be happy ever after.
What about CodeBlocks ?? or Dev C++ ?
These are IDEs. Dev C++, at least when I was using it, was old and not supported - although there was some project to re-animate it, but idk how it goes. I'm using Code::Blocks and I like it. It comes with latest MinGW, so you should be fine if you choose it.
According to wikipedia, Visual C++ 6.0 was released in 1998. If that's the version you have, it is positively antiquated. There are more recent versions available.

The Orwell version of DevC++ is kept up to date and is ok as a recommendation.

Last edited on
Topic archived. No new replies allowed.