array of structs

Discussion in 'C++' started by Kenny McCarty, Jan 28, 2004.

  1. 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
     
    Kenny McCarty, Jan 28, 2004
    #1
    1. Advertising

  2. 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
     
    Michael Mellor, Jan 28, 2004
    #2
    1. Advertising

  3. 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
     
    Karl Heinz Buchegger, Jan 28, 2004
    #3
  4. Kenny McCarty

    Deming He Guest

    Kenny McCarty <> wrote in message
    news:...
    > 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) };
     
    Deming He, Jan 28, 2004
    #4
  5. Kenny McCarty

    Jerry Coffin Guest

    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.
     
    Jerry Coffin, Jan 29, 2004
    #5
  6. 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 :)
     
    Kenny McCarty, Jan 29, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    651
    Al Bowers
    Apr 5, 2004
  2. Chris Hauxwell

    const structs in other structs

    Chris Hauxwell, Apr 23, 2004, in forum: C Programming
    Replies:
    6
    Views:
    564
    Chris Hauxwell
    Apr 27, 2004
  3. Paminu
    Replies:
    5
    Views:
    651
    Eric Sosman
    Oct 11, 2005
  4. Daniel Rudy
    Replies:
    15
    Views:
    1,421
    Keith Thompson
    Apr 10, 2006
  5. Tuan  Bui
    Replies:
    14
    Views:
    494
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page