1. ## classes

Hi,
trying to teach myself C++ and have ran into the following problem......

I have created a class "point" to hold a coordinate. I then have another class "circle" which records a radius and a coordinate - the coordinate instantiated in the "point" class.

The two classes are as follows:-

class point {
private:
int x;
int y;

public:
void init(int x_coord, int y_coord);
void print();
};

class circle {
private:
point centre;

public:
void init(int new_radius, point new_centre);
void print();
point get_centre();
};

I then instantiate the point and the circle as follows....

int main() {
point point1;
circle newCircle;

point1.init(3,4);
newCircle.init(5, point1);

cout << "The coords are : ";
point1.print();

However, when I try to output the centre of the circle (the coordinate) using...

cout << "\tcentre: " << newCircle.get_centre() << "\n";

I get an error message reading...

binary '<<' : no operator defined which takes a right-hand operand of type 'class point' (or there is no acceptable conversion) (new behavior; please see help)

the member functions are as follows:-

void circle::init(int new_radius, point new_centre) {
centre = new_centre;
}

void point::init(int x_coord, int y_coord) {
x = x_coord;
y = y_coord;
}

void point:rint() {
cout << x << "," << y << "\n";
}

}

point circle::get_centre() {
return centre;
}

Any ideas greatly appreciated!!!!!!!

Bill

2. cout doesn't know how to print a class with two members, so it causes an error. So what you need to do is either overload the << operator for point or use its print method.
Code:
```cout << "\tcentre: ";
newCircle.get_centre().print();```

3. that brings up the same error message. Any other ideas?

Thanks,

Bill

4. Sorry, it does work!!!!!!!!!

Thanks,

Bill

5. As a matter of interest, how would I overload the << operator?

6. If you get the same error then you're not doing what I suggested. Here is the code that compiles and runs perfectly for me.
Code:
```#include <iostream>
using namespace std;

class point {
private:
int x;
int y;

public:
void init(int x_coord, int y_coord);
void print();
};

class circle {
private:
point centre;

public:
void init(int new_radius, point new_centre);
void print();
point get_centre();
};

void circle::init(int new_radius, point new_centre) {
centre = new_centre;
}

void point::init(int x_coord, int y_coord) {
x = x_coord;
y = y_coord;
}

void point::print() {
cout << x << "," << y << "\n";
}

}

point circle::get_centre() {
return centre;
}

int main() {
point point1;
circle newCircle;

point1.init(3,4);
newCircle.init(5, point1);

cout << "The coords are : ";
point1.print();

cout << "\tcentre: ";
newCircle.get_centre().print();
}```

7. in public access portion of point class add a line like this to declare the overloaded operator:

friend ostream & operator<<(const ostream & _cout, const point & pt)

and then outside of class provide a definition like one of the two provided below:

ostream & operator<<(const ostream & _cout, const point & pt)
{
pt.print();

or

_cout << pt.getX() << ',' << pt.getY();
}

although the latter requires addition of public accessor functions as well. The latter is what I have seen more often however. Once you have this then you can do this in main() or wherever:

point pt1;
//initialize pt1 somehow;
cout << pt1;

8. oh yeach, and add line:

return _cout;

after whichever line you choose in definition of <<.

Popular pages Recent additions