structure and constant memebers

Discussion in 'C++' started by ravinder thakur, Oct 4, 2005.

  1. hi all experts,

    i have a structure with the constant memebers such as one given below:

    typedef struct {
    const int cbcode;
    int cberror;

    } xtsetplatestaterec;

    now in my function i want to create a stack based object for the same:

    void CreateObjectAndUseIt{
    xtsetplatestaterec plate;
    xtsetplatestaterec plate2 = {0 , 0};

    }

    now this function is giving the compilation errors while compiling on
    vc++ 6.0 since the
    cbcode memeber is a constant. the error is

    error C2512: 'xtsetbleedvaluesrec' : no appropriate default constructor
    available
    error C2552: 'tmpsetplatestaterec' : non-aggregates cannot be
    initialized with initializer list

    can anybody plz explain me what could be done to create instances of
    these objects on stack ???



    strangly this code compiles in the c framework and not in the c++
    framework.
    is there some difference in the way interpertation of const members in
    structures in c
    and c++ language??? and if yes then is there any work around to the
    problem other than removing the const from the strctures.




    thanks
    rt
     
    ravinder thakur, Oct 4, 2005
    #1
    1. Advertising

  2. ravinder thakur

    Milind Guest

    > i have a structure with the constant memebers such as one given below:
    >
    > typedef struct {
    > const int cbcode;
    > int cberror;
    >
    > } xtsetplatestaterec;


    Constant cannot be assigned can only be initialized.
    so simple thing would be :


    struct xtsetplatestaterec {
    const int cbcode;
    int cberror;
    xtsetplatestaterec (int something = 0)
    : cbcode (something ) {}

    }; // we dont need the typedef!! :)

    > void CreateObjectAndUseIt{
    > xtsetplatestaterec plate;

    this will work.
    > xtsetplatestaterec plate2 = {0 , 0};


    what are you trying to init ?? an agregate??
    > }
    >
    >

    hth
    ~M
     
    Milind, Oct 4, 2005
    #2
    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. Christopher M. Lusardi
    Replies:
    1
    Views:
    4,111
  2. G Patel
    Replies:
    8
    Views:
    360
  3. structure and constant memebers

    , Oct 4, 2005, in forum: C Programming
    Replies:
    13
    Views:
    524
    Tim Rentsch
    Oct 26, 2005
  4. Replies:
    4
    Views:
    348
    Keith Thompson
    Dec 14, 2006
  5. G G
    Replies:
    3
    Views:
    99
    Ben Bacarisse
    Apr 20, 2014
Loading...

Share This Page