Help for void Triangle::print() class function

//Point.cpp
#include "Point.h"
#include <iostream>
#include <cmath>
using namespace std;

Point::Point() { //Initialise the point to the origin.
}

Point::Point(int x, int y) { //Point object initailised to given coordinates.
_x = x;
_y = y;
}

double Point::distanceTo(Point point2) { //Calculates distance between Point and point 2.
double x_dist = _x - point2._x;
double y_dist = _y - point2._y;
return sqrt(x_dist*x_dist + y_dist*y_dist);
}

void Point::print() { //Prints out point in (x, y) format.
cout << "(" << _x << ", " << _y << ")" << endl;
}

//Triangle.cpp
#include "Triangle.h"
#include "Point.h"
#include <iostream>
#include <cmath>
using namespace std;

Triangle::Triangle() { //Initialise the vertices to have zero for all coordinates.
}

double Triangle::perimeter(Point point1, Point point2, Point point3) {
return (point1.distanceTo(point2) + point2.distanceTo(point3) + point3.distanceTo(point1));
}

double Triangle::area(Point point1, Point point2, Point point3) {
double a, b, c, S; //Variables to store parts of Hero's/Heron's formula.

a = point1.distanceTo(point2);
b = point2.distanceTo(point3);
c = point3.distanceTo(point1);
S = (a + b + c)/2;

return sqrt(S*(S-a)*(S-b)*(S-c)); //Hero's/Heron's formula of coordinate geometry to calculate area of a triangle.
}

void Triangle::print() { //print out the Triangle with the format "( (x1, y1), (x2, y2), (x3, y3) )"
How do I accomplish this??
}

When i test with cout << _point1.print(), there's an error:
[Error] no match for 'operator<<' in 'std::cout << ((Triangle*)this)->Triangle::_point1.Point::print()'

Any help would be greatly appreciated.
Last edited on
You need to overload operator<< when you want to use it with a non built-in type. There is an example in the following link

http://msdn.microsoft.com/en-us/library/1z2f6c2k.aspx

One more thing, don't use the underscore to prefix variables/functions, because some of these are preserved for internal use by many library headers. If you decide to use underscore, use x_ (and not _x).
One more thing, don't use the underscore to prefix variables/functions, because some of these are preserved for internal use by many library headers. If you decide to use underscore, use x_ (and not _x).


All of them are reserved inside the global namespace. As long as your usage is outside of that namespace, using a single underscore followed by a lower case letter (_x) is perfectly fine.

http://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier
Last edited on
@cire, thank you for the clarification.
Much thanks to both.
Topic archived. No new replies allowed.