structure and constant memebers

  • Thread starter ravinder thakur
  • Start date
R

ravinder thakur

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
 
M

Milind

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
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top