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;
        }
};

Is the PointArray actually being expanded when you use the [] operator?

It should? I want it to be dynamic like a vector so that it pushes the next point into it..

When you step through the code in the debugger, is it (do you see it) expanded?

I don't exactly know what you mean by "Expanded". That can be dynamically or allocated previously.

When I step through, it prints:

PointArray P Size = 10    //This is correct because I did PointArray P[10];
P[0] = (1, 2);            //This is correct because I assigned it a point;

So yes it holds the values but I cannot allocate a dynamic array of Points.

If you'd like to run/test/read any of the entire source, see below:

Test Program:

#include "Types.h"
#include <iostream>
#include <vector>

using namespace std;

int main()
{
    Point P(1, 2);
    Point Q(3, 4);

    PointArray Z[10];
    writeln(LEN(Z));

    Z[0] = P;
    Z[1] = Q;

    writeln(*Z[0]);     //NOT sure why I have to use *Z to deference AGAIN?!
    writeln(*Z[1]);

    cin.get();
}

Types.h

#ifndef TYPES_H_INCLUDED
#define TYPES_H_INCLUDED
#include <math.h>
#include <vector>
#include <assert.h>
#include <sstream>

using namespace std;


/** Definitions **/
#define LEN(a) (sizeof(a)/sizeof(*a))
#define HIGH(a) (sizeof(a)/sizeof(*a) - 1)

#define Repeat do{
#define Until(condition) }while(!(condition));

#define writeln(Output) (cout<<Output<<"\n");

/** Types **/

struct Box;
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 ostream& operator << (ostream& Str, const Point &PT)      //For use with Writeln & cout.
    {
        Str<<"("<<PT.X<<", "<<PT.Y<<")";
        return Str;
    }

    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 Box
{
    int X1, Y1, X2, Y2;
    Box(){X1 = Y1 = X2 = Y2 = 0;};                                                      //Default Constructor.
    Box(int X1_, int Y1_, int X2_, int Y2_) : X1(X1_), Y1(Y1_), X2(X2_), Y2(Y2_) {};    //Alternate Constructor.
    ~Box(){};                                                                           //Destructor.

    friend int operator == (const Box &B, const Box &B2)                          //Are the Boxes the same?
    {
        return ((B.X1 == B2.X1) && (B.X2 == B2.X2) && (B.Y1 == B2.Y1) && (B.Y2 == B2.Y2));
    }

    friend inline int operator != (const Box &B, const Box &B2)                                               //Are the Boxes Not the same?
    {
        return !(B == B2);
    }

    Box operator += (Box B)
    {
        X1 += B.X1;
        Y1 += B.Y1;
        X2 += B.X2;
        Y2 += B.Y2;
        return *this;
    }

    Box operator -= (Box B)
    {
        X1 -= B.X1;
        Y1 -= B.Y1;
        X2 -= B.X2;
        Y2 -= B.Y2;
        return *this;
    }

    Box PointToBox(Point UpperLeft, Point LowerRight)
    {
        return Box(UpperLeft.X, UpperLeft.Y, LowerRight.X, LowerRight.Y);
    }

    Box& operator = (const Box& B)
    {
        if (this != &B)
        {
            X1 = B.X1;
            Y1 = B.Y1;
            X2 = B.X2;
            Y2 = B.Y2;
        }
        return *this;
    }

    friend ostream& operator << (ostream& Str, const Box &B)                                  //For use with Writeln & cout.
    {
        Str<<"("<<B.X1<<", "<<B.Y1<<", "<<B.X2<<", "<<B.Y2<<")";
        return Str;
    }

    friend inline Box operator + (Box B1, int S)                                              //Increase Box Size.
    {
        return Box(B1.X1 + S, B1.Y1 + S, B1.X2 + S, B1.Y2 + S);
    }

    friend inline Box operator - (Box B1, int S)                                              //Decrease Box Size.
    {
        if (((B1.X1 - S) < 0) || ((B1.Y1 - S) < 0) || ((B1.X2 - S) < 0) || ((B1.Y2 - S) < 0))
            return Box(-1, -1, -1, -1);                                                     //Return -1's if its not a valid box.
        else if (((B1.X1 - S) == 0) || ((B1.Y1 - S) == 0) || ((B1.X2 - S) == 0) || ((B1.Y2 - S) == 0))
            return Box(0, 0, 0, 0);                                                         //Returns 0's because it becomes a point.
        else
            return Box(B1.X1 + S, B1.Y1 + S, B1.X2 + S, B1.Y2 + S);                         //Return the resized box.
    }
};

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 Point& PT)
        {
            if (this[LEN(this)] != &PT)                    //I think the LEN(this)  is correct?
            {
                P[LEN(this)] = PT;
            }
            return *this;
        }

        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;
        }
};

Point Point::MidPointBox(const Box& B)
{
    return Point(((B.X1 + B.X2)/2), ((B.Y1 + B.Y2)/2));
};


#endif //TYPES_H_INCLUDED
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.