473,386 Members | 1,733 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Struct Initialization

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
Jul 11 '08 #1
8 1856
Travis wrote:
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;
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);

etc.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 11 '08 #2

"Travis" <tr***********@gmail.comwrote in message
news:d7**********************************@25g2000h sx.googlegroups.com...
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
struct Point
{
int x;
int y;
public:
Point(int xx = 0, int yy = 0) : x(xx), y(yy) { }
};

int main()
{
Point Lines[3];
Lines[0] = Point(0, 1);
Lines[1] = Point(1, 2);
Lines[2] = Point(2, 3);
return 0;
}
-Mike
Jul 11 '08 #3

"Travis" <tr***********@gmail.coma écrit dans le message de news:
d7**********************************...oglegroups.com...
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);

-----------------------

Eric Pruneau
Jul 11 '08 #4
Victor Bazarov wrote:
>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).


Jul 11 '08 #5
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.
Jul 14 '08 #6
Travis wrote:
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.
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 14 '08 #7
On Jul 14, 1:34*pm, Victor Bazarov <v.Abaza...@comAcast.netwrote:
Travis wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Ah ha. Fantastic. Thank you very much. Sometimes another pair of eyes
is worth millions.

Thanks again
Jul 14 '08 #8
Travis wrote:
[..] Sometimes another pair of eyes
is worth millions.
:-) If I had a nickel every time I heard that...

You're welcome!

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 14 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: sathyashrayan | last post by:
The standard confirms that the following initialization of a struct struct node { --- --- } struct node var = {NULL};
10
by: emma middlebrook | last post by:
Hi I discovered that if you declare a structure (and not 'new()' it) you can then separately initialize its members and the compiler counts those separate statements as a full initialization....
3
by: Karl M | last post by:
Hi everyone, I just notice some strange behaviors on the MS C++ compiler regarding struct default constructor, see the example bellow: struct MyStruct { int a; }; class MyClass { public:
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
6
by: arnuld | last post by:
this one was much easier and works fine. as usual, i put code here for any further comments/views/advice: --------- PROGRAMME ------------ /* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a...
18
by: Ehud Shapira | last post by:
Is it possible to have a declaration of a struct pointer initialized to an unnamed struct? (I'm only concerned with static/global variables, if it matters.) I'm trying to do something like: ...
6
by: Daniel Rudy | last post by:
Hello Group. Please consider the following code: /* this table is used in the wipedevice routine */ static const struct wipe_t { uchar wte; /* wipe table entry */ } wipetable = {...
2
by: dj3vande | last post by:
Is this code, as the complete contents of a translation unit, valid C90 that initializes the struct foo to contain copies of the values passed to bar()? -------- struct foo { int i; void *v;...
10
by: Juha Nieminen | last post by:
I suppose you can never know C++ fully. I would have never guessed this actually compiles and works: struct Point { int x, y; }; struct Line { Point endpoint; int weight; };
5
by: ssylee | last post by:
I'm not sure if I can initialize members of a struct the lazy way. For example, let's say I have a struct defined as below: typedef struct _ABC { BOOL A; BOOL B; BOOL C; } ABC;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.