Sorry for using Inception in the title but it's the only way I think I can describe it..

Take this for example:

Definition.H

struct XY
{
  string info;
  string name;
  string tag;
  string attrib;
}; //is it better to create an object here inorder to make the array?

struct JK
{
  XY* Me;
  friend ostream& operator << (ostream& Str, const JK &Meh)
  {
     for (int I = 0; I < sizeof(Meh)/sizeof(Meh[0]); I++)
     {
        Str<<" "<<Meh[I]<<" ";
     }
     return Str;
  }
}

Struct JK creates an array of Struct XY.. Now when I try to print out the contents of the Array.. it returns 1 for the size every single time! Why? Is my math wrong? Am I doing something wrong? Why is sizeof(Meh)/sizeof(Meh[0]).. 1? I just want it to print all the contents of the array.

In my program I have:

JK P[10].. Shouldn't the size be 10? or the amount of elements?
In my program, I can access each element of the array and print them like:
cout<< *P[0]<<*P[1].. etc..

But I cannot do that in the above code in the Definition.H.

Edited 4 Years Ago by triumphost: n/a

sizeof(any pointer here) is always the size of a pointer, on 32-bit compilers it is 4 (bytes). The sizeof operator returns the number of bytes occupied by whatever object you give it, in this case you give it a pointer.

>>Shouldn't the size be 10? or the amount of elements?
No. See above. If you need to know the number of elements in the array you will have to keep track of it in another int variable. sizeof operator will not give you that information.

Edited 4 Years Ago by Ancient Dragon: n/a

But how can I keep track of the array length without having to pass an int in my actual .cpp file?

I've tried:

struct JK
{
  private:
     XY* Me;
     int ALen;
  public:
     JK(){ALen = sizeof(Me)/sizeof(ME[0]);}

  friend ostream& operator << (ostream& Str, const JK &Meh)
  {
     for (int I = 0; I < ALen; I++)         //Put ALen here..
     {
        Str<<" "<<Meh[I]<<" ";
     }
     return Str;
  }
}

But it still gives me problems..

Try this: And please call it a class instead of struct. Yes, they are both nearly the same, but class if preferred.

class JK
{
  private:
     XY* Me;
     int ALen;
  public:
     JK()
     {
        ALen = 1;
        ME = new XY[aLen];
     }

  friend ostream& operator << (ostream& Str, const JK &Meh)
  {
     for (int I = 0; I < ALen; I++)         //Put ALen here..
     {
        Str<<" "<<Meh[I]<<" ";
     }
     return Str;
  }
}

Edited 4 Years Ago by Ancient Dragon: n/a

Doesn't work.. I get the same error "Invalid use of non-static member..
I don't think it's possible.. maybe instead of doing XY* I can do a vector?

Edit: Tried the vector and failed..

Edited 4 Years Ago by triumphost: n/a

Line 17: Meh is not an array. You probably mean Me. The code below compiles ok.

class JK
{
  private:
     XY* Me;
     int ALen;
  public:

	  JK()
     {
        ALen = 1;
        Me = new XY[ALen];
     }
  friend ostream& operator << (ostream& Str, const JK &Meh)
  {
     for (int I = 0; I < Meh.ALen; I++)         //Put ALen here..
     {
        Str<<" "<<Meh.Me[I]<<" ";
     }
     return Str;
  }
};

Or you could add operator[] so that you can do it like this

{
  private:
     XY* Me;
     int ALen;
  public:

	  JK()
     {
        ALen = 1;
        Me = new XY[ALen];
     }
	  const XY& operator[](int i) { return Me[i];}
  friend ostream& operator << (ostream& Str, JK &Meh)
  {
     for (int I = 0; I < Meh.ALen; I++)  
     {
        Str<<" "<<Meh[I]<<" ";
     }
     return Str;
  }
};

Edited 4 Years Ago by Ancient Dragon: n/a

I added that operator [].. Still the Length returns 1 all the time :S I tried the aLen and it still kept returning 1 so I removed it since its the same as it was before. Here is my actual .CPP file if your want to see.

#include <math.h>
#include <vector>
#include <assert.h>
#include <sstream>
#include <stdarg.h>
#include <iostream>

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 **/

class Point
{
    private:
        int X, Y;

    public:
        Point() : X(0), 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;
        }

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

class PointArray
{
    private:
        Point* P;    //would prefer if this was a vector.
        int rows;

    public:

        PointArray(int I = 1) : P(I > 0 ? new Point[I] : 0), rows(I) {}   //contructor.. Make a new PointArray if I > 0.
        ~PointArray()                                                     //Destructor..
        {
            delete[] P;                                 //The new operator was used.. Must delete all instances of P.
            P = 0;
            rows = 0;
        }

        Point& operator ()(int I)
        {
            return P[I];                            //Return A Point.
        }

        const Point& operator()(int I) const
        {
            return P[I];
        }

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

        PointArray& operator = (const Point& PT)
        {
            if (this[LEN(this)] != &PT)
            {
                P[LEN(this)] = PT;
            }
            return *this;
        }

        friend ostream& operator << (ostream& Str, const PointArray &PA)                                  //For use with Writeln & cout.
        {
            Str<<"[";
            for (int I = 0; I < PA.rows; I++)
            {
                Str<<PA[I]<<",";
            }
            Str<<"]";
            return Str;
        }
};


int main()
{
    Point P(1, 2);
    Point Q(3, 4);
    Point X(5, 6);
    Point Y(7, 8);

    PointArray PT[5];
    PT[0] = P;
    PT[1] = Q;
    PT[2] = X;
    PT[4] = Y;

    writeln(*PT<<"\n");       //Dereference PT.  Cannot Remember Why.  For some reason, only prints the first point. Needs to print ALL points.

    cout<<"[";

    for (int I = 0; I < LEN(PT); I++)
        cout<< *PT[I] <<", ";

    cout<<"]\n";

    writeln("\nDone.");
    cin.get();
}
This article has been dead for over six months. Start a new discussion instead.