In pascal, you can have something like the following:

type
TPoint = record
X, Y: Integer;
end;
TPointArray = array of TPoint;
T2DPointArray = array of TPointArray;

In C++ I tried to replicate this and failed miserably.

struct Point
{
X: Integer;
Y: Integer;
};

struct PointArray
{
//Array of Points? :S
//Don't know if to use vectors or not or even how to do this..

vector<Point>X;
}

And I want to be able to use it like so:

int main()
{
Point P;
P(X, Y);
}

How can I do this?

to declare variables in c++ you do it as

int x, y;

So with that to declare a structure with an x and y variable you would do

struct Point
{
int x, y;
}

Finally if you want to constuct you struct with …

it is simple:

Then why make it so complicated? How about:

#include <iostream>
struct Point             // use proper formatting
{
int X;
int Y;
} ;
typedef  Point* PointsArr;

int main(void)                      // use proper main() footprint
{
PointsArr test;             // hold 10 points
for(int i = 0; …

All 7 Replies

to declare variables in c++ you do it as

int x, y;

So with that to declare a structure with an x and y variable you would do

struct Point
{
int x, y;
}

Finally if you want to constuct you struct with values when you make it you need to add a constuctor

struct Point
{
int x, y;
Point(int x_, int y_) : x(x_), y(y_) {}
}

int main()
{
int x = 5, y = 6;
Point point(x, y);
}
commented: EXACTLY WHAT I WANTED!!!!! +6

it is simple:

#include <iostream>
struct Point{   int X;   int Y;};
typedef  Point* PointsArr;
static const int size=100;
void main()
{
PointsArr test=new Point[size];
memset(test,0,size*sizeof(Point));
for(int i=0;i<size;i++)
{
test[i].X=i*2;
test[i].Y=(i*2)+1;
}
for(int i=0;i<size;i++)
{
std::cout<<test[i].X<<" "<<test[i].Y<<std::endl;
}
}

The first answer was what I wanted :) But it was only answering one question..

It works wonders for calling Points like how he did it but I want to define a type called PointArray which creates an array of Points and you can set the size of it later on somewhere in the code. It's my first time working with structs and classes so I have not a single clue what I'm doing.. I read tutorials on CPlusPlus.com but it doesn't tell me how to create an array of a custom type and be able to reference it like the first guy that commented. I'm looking into constructors now that he's mentioned it.
I still mix up when to use -> vs . and ::

//So like:

struct PointArray
{
vector<Point>P;   //Or dynamic array of Points.
P(int X_, Y_) : X(X_), Y(Y_) {}   //<--- didn't know how to define it there?
}

//So I can use it like:

int main()
{
PointArray P;
Memset(Point, 5, size*sizeof(Point));

P = [point(1, 2), point(3, 4)];
P = Point(0, 0);
P.X = 3;
P.Y = 4;
}

it is simple:

Then why make it so complicated? How about:

#include <iostream>
struct Point             // use proper formatting
{
int X;
int Y;
} ;
typedef  Point* PointsArr;

int main(void)                      // use proper main() footprint
{
PointsArr test;             // hold 10 points
for(int i = 0; i < size; i++)   // use whitespace to more easily read the code
{
test[i].X = i * 2;          // use whitespace
test[i].Y = (i * 2) + 1;    // use whitespace
}
for(int i = 0; i < 10; i++)
{
std::cout << test[i].X << " " << test[i].Y << std::endl;
}
return 0;                       // end main() properly
}

Line 6 is a special constructor syntax, used if all you want to do in a constructor is assign values. Donno how to explain it much further without copying a longer text on constructors, classes and so on...

Second, for that code to work, you have to define the Point class somewhere.

Third, P is of type PointArray which has a member P, so to acces a single point inside your struct, you have to write P.P[pnt_index]

Fourth, not memset... why would you even want to use it there?
More like P.P.resize(size) to set the length of the array (if that's what you want to do)

And then to set the point coordinates use P.P[index].X=val; and P.P[index].Y=val;

did you tried this code? WaltP it will not compile !!

PointsArr test;   //2d Poinetrs array
Point test;   //1d Poinetrs array

yes i made it some difficult but more readable (Magic number 10??)

Ok.. I went to the drawing board and I tried! I just can't understand what you guys mean.. I don't want to have to do it like that..

I want to sorta do it like the below.. and I want to know if I need a default constructor + a destructor.. Mine doesn't work.

#include <iostream>
#include <vector>

using namespace std;

struct Point
{
int X, Y;
Point() {X = 0; Y = 0;};  //Default constructor?
Point(int X_, int Y_) : X(X_), Y(Y_) {}
//~Point() {delete Point}   Why doesn't it work?
};

struct Box
{
int X1, Y1, X2, Y2;
Box(int X1_, int Y1_, int X2_, int Y2_) : X1(X1_), Y1(Y1_), X2(X2_), Y2(Y2_) {}
};

struct PointArray
{
vector<Point>P;
PointArray[vector<Point>P_] : P[P_] {};
};

int main()
{
Point P(2, 3);
Box A(2, 4, 1, 1);
PointArray PA;
PA.X = 1;
PA.Y = 2;
PA = [Point(10, 1), Point(3, 2), Point(0, 4)];
}
Be a part of the DaniWeb community

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