I belive the answer to this question is really simple, but I just can't get this thing working properly. I have basically created two classes; one for points and one for polygons. Polygons consists of a dynamic list of points.
However, when i try to overload the + operator in the points class and make it return a polygon of the two points i get som weird output and a "Debug assertion failed" after i close the console window.
Here is the + operator overloading method:
CPolygon CPoint::operator + (CPoint pointToAdd) {
CPolygon polygon;
polygon.addPoint(this);
polygon.addPoint(pointToAdd);
cout << polygon.toString();
return polygon;
}
When i now try to use this method I get the following output for instance:
(5, 2, 3) - (1, 1, 2)
(444417074, -33686019, , -1555471217) - (-1424299942, 0, 0)
The first of the ouputed lines is from the method itself, whereas the 2nd line is from the place where the polygon is being returned to.
I really have noe idea what is happening to my polygon object on the way from being inside the method to its returning way to the calling code.
I'd be very thankful if anyone could give me a bit of insight on this one :)
EDIT
Here are the addPoint methods of the polygon class:
void CPolygon::addPoint(CPoint pointToAdd) {
nrOfPoints++;
// Create a temp array of the new size
CPoint * tempPoints = new CPoint[nrOfPoints];
// Copy all points to the temp array
for(int i = 0; i < (nrOfPoints - 1); i++) {
tempPoints[i] = points[i];
}
// Add the new point to the end of the array
tempPoints[nrOfPoints - 1] = pointToAdd;
// Delete the old array and set the temp array as the new array
delete[] points;
points = tempPoints;
}
void CPolygon::addPoint(CPoint * pointToAdd) {
addPoint(* pointToAdd);
}