Struct Initialization

T

Travis

struct Point
{
int x;
int y;
};

Point Lines[3] =
{
{0,1},
{1,2},
{2,3}
}


The above works fine. What I'd like to do is declare Lines first and
then populate the array later without having to individually add .x =
and .y= for every attribute.

Point Lines[3];

Lines[0] = {0,1};

This doesn't work. Is there someway to do that though without

Point Lines[3];
Lines[0].x=0;
Lines[0].y=1;

Thanks
 
E

Eric Pruneau

Travis said:
struct Point
{
int x;
int y;
};

Point Lines[3] =
{
{0,1},
{1,2},
{2,3}
}


The above works fine. What I'd like to do is declare Lines first and
then populate the array later without having to individually add .x =
and .y= for every attribute.

Point Lines[3];

Lines[0] = {0,1};

This doesn't work. Is there someway to do that though without

Point Lines[3];
Lines[0].x=0;
Lines[0].y=1;

Thanks

No but you can add a SetPoint function

struct Point
{
int x;
int y;
void SetPoint(int NewX, int NewY) { x=NewX; y = NewY; }
}


Point Lines[3];
Lines[0].SetPoint(0,1);
 
R

Rolf Magnus

Victor said:
Point Lines[3];

Lines[0] = {0,1};

This doesn't work. Is there someway to do that though without

Point Lines[3];
Lines[0].x=0;
Lines[0].y=1;

No. There are no "aggregate literals" in C++ yet. You can, of course
create a pseudo-constructor (or a "factory function"):

Point createPoint(int x, int y) {
Point p = { x, y };
return p;
}

and then do

Lines[0] = createPoint(0,1);
Lines[1] = createPoint(1,2);

Or simply a regular constructor (if the struct doesn't have to be POD).
 
T

Travis

So I'm not sure what I'm missing.

Here's my test http://paste.cbwhiz.com/281

If I uncomment line 27 and comment out 28-30, everything works.

As it is though, I get a link error (I believe) that says : undefined
reference to `Point::point[in-charge]()'

Also, I have tried class and struct, both produce the same results.
 
T

Travis

Travis said:
So I'm not sure what I'm missing.
Here's my testhttp://paste.cbwhiz.com/281
If I uncomment line 27 and comment out 28-30, everything works.
As it is though, I get a link error (I believe) that says : undefined
reference to `Point::point[in-charge]()'
Also, I have tried class and struct, both produce the same results.

You declared the default c-tor in 'Point', but never defined it.  When
you define an array of Point and provide no initialisers, the default
c-tor is *used*.  Since no definition exists, the linker cannot find the
function.  Change the constructors from

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

to
         explicit Point(int x_ = 0, int y_ = 0) : x(x_), y(y_) { }

(thus making your constructor both parameterised AND default).

V

Ah ha. Fantastic. Thank you very much. Sometimes another pair of eyes
is worth millions.

Thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,173
Messages
2,570,938
Members
47,475
Latest member
NovellaSce

Latest Threads

Top