Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > array of structs

Reply
Thread Tools

array of structs

 
 
Kenny McCarty
Guest
Posts: n/a
 
      01-28-2004
I am trying to create an array of structs and I am having problems
adding the values into the structure itself. Here is the code:

struct predefinedCaptureSetting
{
int buffer_signal;
int transform_signal;
bool only_solid;
bool line_as_solid;
bool no_texture;
bool optimize_geometry;
bool recompute_normals;
bool last_frame;
};

predefinedCaptureSetting PredefinedSetting[25];
PredefinedSetting[0] = {1,0,false,false,false,true,true,false};
PredefinedSetting[1] = {1,1,false,false,false,true,true,false};
PredefinedSetting[2] = {0,1,false,false,false,true,true,false};
....
....
PredefinedSetting[22] = {1,1,false,false,false,true,true,false};
PredefinedSetting[23] = {1,0,false,false,false,true,true,false};
PredefinedSetting[24] = {1,1,false,false,false,true,true,false};


Is there any reason why the above code wouldn't work? Thanks for any
help
 
Reply With Quote
 
 
 
 
Michael Mellor
Guest
Posts: n/a
 
      01-28-2004
Kenny McCarty wrote:
> I am trying to create an array of structs and I am having problems
> adding the values into the structure itself. Here is the code:
>
> struct predefinedCaptureSetting
> {
> int buffer_signal;
> int transform_signal;
> bool only_solid;
> bool line_as_solid;
> bool no_texture;
> bool optimize_geometry;
> bool recompute_normals;
> bool last_frame;
> };
>
> predefinedCaptureSetting PredefinedSetting[25];
> PredefinedSetting[0] = {1,0,false,false,false,true,true,false};
> PredefinedSetting[1] = {1,1,false,false,false,true,true,false};
> PredefinedSetting[2] = {0,1,false,false,false,true,true,false};
> ...
> ...
> PredefinedSetting[22] = {1,1,false,false,false,true,true,false};
> PredefinedSetting[23] = {1,0,false,false,false,true,true,false};
> PredefinedSetting[24] = {1,1,false,false,false,true,true,false};
>
>
> Is there any reason why the above code wouldn't work? Thanks for any
> help


Try the following:

predefinedCaptureSetting PredefinedSetting[25] =
{
{1,0,false,false,false,true,true,false},
{1,1,false,false,false,true,true,false},
{0,1,false,false,false,true,true,false},
....
{1,1,false,false,false,true,true,false},
{1,0,false,false,false,true,true,false},
{1,1,false,false,false,true,true,false}
};

Michael Mellor
 
Reply With Quote
 
 
 
 
Karl Heinz Buchegger
Guest
Posts: n/a
 
      01-28-2004
Kenny McCarty wrote:
>
> I am trying to create an array of structs and I am having problems
> adding the values into the structure itself. Here is the code:
>
> struct predefinedCaptureSetting
> {
> int buffer_signal;
> int transform_signal;
> bool only_solid;
> bool line_as_solid;
> bool no_texture;
> bool optimize_geometry;
> bool recompute_normals;
> bool last_frame;
> };
>
> predefinedCaptureSetting PredefinedSetting[25];
> PredefinedSetting[0] = {1,0,false,false,false,true,true,false};
> PredefinedSetting[1] = {1,1,false,false,false,true,true,false};
> PredefinedSetting[2] = {0,1,false,false,false,true,true,false};
> ...
> ...
> PredefinedSetting[22] = {1,1,false,false,false,true,true,false};
> PredefinedSetting[23] = {1,0,false,false,false,true,true,false};
> PredefinedSetting[24] = {1,1,false,false,false,true,true,false};
>
> Is there any reason why the above code wouldn't work?


Sure.
The thing on the right side of the assignment isn't a struct
it is just a list of numbers and boolean values seperated
by comma and enclosed in { }.

But you can change that.
Give that structure a constructor (Surprise: yes you can do that.
In C++ there is no difference of a class and a struct except that
in a struct everything is public while in a class it is private and
inheritance is again public for structs by default while it is
private for a class).

struct predefinedCaptureSetting
{
predefinedCaptureSetting( int buffer, int transform, bool only, bool line, bool no_text,
bool opt, bool recompute, bool last )
: buffer_signal( buffer ),
transform_signal( transform ),
only_solid( only ),
line_as_solid( line ),
no_texture( no_text ),
optimize_geometry( opt ),
recompute_normals( recompute ),
last_frame( last )
{}

int buffer_signal;
int transform_signal;
bool only_solid;
bool line_as_solid;
bool no_texture;
bool optimize_geometry;
bool recompute_normals;
bool last_frame;
};

Then you can write:

PredefinedSetting[0] = predefinedCaptureSetting( 1,0,false,false,false,true,true,false );


May I ask which text book you are using?
Just that we start warning others about a C++ textbook which
hasn't a chapter on constructors.

--
Karl Heinz Buchegger

 
Reply With Quote
 
Deming He
Guest
Posts: n/a
 
      01-28-2004
Kenny McCarty <> wrote in message
news: om...
> I am trying to create an array of structs and I am having problems
> adding the values into the structure itself. Here is the code:
>
> struct predefinedCaptureSetting
> {
> int buffer_signal;
> int transform_signal;
> bool only_solid;
> bool line_as_solid;
> bool no_texture;
> bool optimize_geometry;
> bool recompute_normals;
> bool last_frame;
> };
>
> predefinedCaptureSetting PredefinedSetting[25];
> PredefinedSetting[0] = {1,0,false,false,false,true,true,false};
> PredefinedSetting[1] = {1,1,false,false,false,true,true,false};
> PredefinedSetting[2] = {0,1,false,false,false,true,true,false};
> ...
> ...
> PredefinedSetting[22] = {1,1,false,false,false,true,true,false};
> PredefinedSetting[23] = {1,0,false,false,false,true,true,false};
> PredefinedSetting[24] = {1,1,false,false,false,true,true,false};
>
>
> Is there any reason why the above code wouldn't work? Thanks for any
> help


Note that predefinedCaptureSetting's fields are shrinked and the array,
PredefinedSetting's size reduced just to show an example.

struct predefinedCaptureSetting
{
int buffer_signal;
int transform_signal;
bool only_solid;

predefinedCaptureSetting(int buf = 1, int tra = 1, bool onl = false) :
buffer_signal(buf), transform_signal(tra), only_solid(onl){}
};

predefinedCaptureSetting PredefinedSetting[3] =
predefinedCaptureSetting(1,0,false),
predefinedCaptureSetting(0,0,false),
predefinedCaptureSetting(0,1,false) };


 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      01-29-2004
In article < >,
says...

[ ... ]

> predefinedCaptureSetting PredefinedSetting[25];
> PredefinedSetting[0] = {1,0,false,false,false,true,true,false};
> PredefinedSetting[1] = {1,1,false,false,false,true,true,false};
> PredefinedSetting[2] = {0,1,false,false,false,true,true,false};


This syntax only works for initialization, not assignment, so you can do
this:

predefinedCaptureSetting PredefinedSetting[25] = {
{1, 0, false, false, false, true, true, false},
{1, 1, false, false, false, true, true, false},

and so on for all 25 items, or you can do something like:

PredefinedSetting[0] =
PredefinedCaptureSetting(1, 0, false, false, false, true, true, false);

and so on for 25 values.

On a different note, I think I'd define things a bit differently. I'd
probably define the bools as an int (or short, or whatever) and define
some names for bit positions, so your first item would be something
along this line:

Predef[0] = Setting(1, 0, OPTIMIZE_GEOMETRY | RECOMPUTE_NORMALS);

At least to me, "OPTIMIZE_GEOMETRY | RECOMPUTE_NORMALS" seems quite a
bit more meaningful than "false, false, false, true, true, false". The
latter is meaningful ONLY if somebody has memorized the order in which
you've arranged the fields -- and even then, you just about have to
count over to be sure whether a particular "true" is in the third or
fourth position (and adding even a couple more possibilities renders it
much more difficult still).

--
Later,
Jerry.

The universe is a figment of its own imagination.
 
Reply With Quote
 
Kenny McCarty
Guest
Posts: n/a
 
      01-29-2004
Thank you everyone for your help. It has been a long time since I
last did any C++ coding so I was a little but rusty
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Packed structs vs. unpacked structs: what's the difference? Daniel Rudy C Programming 15 04-10-2006 08:10 AM
Array of structs instead of an array with pointers to structs? Paminu C Programming 5 10-11-2005 07:18 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
const structs in other structs Chris Hauxwell C Programming 6 04-27-2004 07:03 PM
structs with fields that are structs Patricia Van Hise C Programming 5 04-05-2004 01:37 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57