The below code is what I have.. It's a custom type I made called Points that stores co-ordinates on the screen.. The problem is in the PointsArray Struct. When I declare one like so:
PointArray P;
Point A(10, 5);
for (int I = 0; I < 5; I++)
{
P[I] = A;
cout<<P[I].X;
}
It terminates the process with a status of 3.. The Points Array struct is supposed to make a vector/dynamic array of Points. What causes this error 3?
struct Point
{
int X, Y;
Point() {X = Y = 0;}; //Default constructor.
Point(int X_, int Y_) : X(X_), Y(Y_) {}; //Alternate constructor.
~Point(){}; //Destructor.
int operator == (const Point &PT) const //Are the points Equal?
{
return ((X == PT.X) && (Y == PT.Y));
}
int operator != (const Point &PT) const //Are the points Not equal?
{
return ((X != PT.X) && (Y != PT.Y));
}
Point& operator = (const Point& PT)
{
if (this != &PT)
{
X = PT.X;
Y = PT.Y;
}
return *this;
}
Point operator += (Point PT)
{
X += PT.X;
Y += PT.Y;
return *this;
}
Point operator -= (Point PT)
{
X -= PT.X;
Y -= PT.Y;
return *this;
}
Point MidPointBox (const Box &B);
friend inline Point operator + (Point P1, Point P2) //Add two points.
{
return Point(P1.X + P2.X, P1.Y + P2.Y);
}
friend inline Point operator - (Point P1, Point P2) //Subtract two points.
{
return Point(P1.X - P2.X, P1.Y - P2.Y); //No absolute value needed since points can have negative values.
}
friend inline int Distance(Point P1, Point P2) //Friend is for Inheritance and friendship of a Class/Struct.
{
return (hypot(P1.X - P2.X, P1.Y - P2.Y));
}
friend inline Point Invert(Point PT) //Invert a point.
{
return Point(-PT.X, -PT.Y);
}
};
struct PointArray
{
private:
Point* P;
int rows;
public:
Point& operator ()(int I)
{
return P[I]; //Return A Point.
}
Point& operator[](int I)
{
assert(P != 0 && I >= 0 && I < rows); //P <> 0 & I Must be >=0 & I < num of rows. Asserts to terminate the program upon Bad Input.
return P[I]; //Return A Point.
}
const Point& operator[](int I) const
{
assert(P != 0 && I >= 0 && I < rows); //P <> 0 & I Must be >=0 & I < num of rows.
return P[I]; //Return A Point.
}
int Rows() const
{
return rows;
}
operator const Point* () const
{
return P;
}
operator Point* ()
{
return P;
}
Point* Resize(int NewSize);
PointArray& operator = (const PointArray &N);
PointArray(const PointArray &N);
PointArray(int I = 2):
P(I > 0 ? new Point[I] : 0), rows(I) { //Created a New Point.
}
~PointArray()
{
delete[] P; //The new operator was used.. Must delete all instances of P.
P = 0;
rows = 0;
}
};