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)