struct with constructor: Compiler Warning.

S

silversurfer2025

Hello everyone,
I am using a struct inside another class, which can be used through an
empty constructor or another one. The definition looks like this:

struct Entry{
int index; //line 38
FeatureVector* value; //line 39
int object;
int count;
double variance;
Entry() {}
Entry(FeatureVector* _value, int _index, int _object, int _count,
double _variance)
: value(_value), index(_index), object(_object), count(_count),
variance(_variance) {}
};

When I am compiling I get the following error:

.../ModelContainer.h: In constructor 'Entry::Entry(FeatureVector*,
int, int, int, double)':
.../ModelContainer.h:39: warning: 'Entry::value' will be initialized
after
.../ModelContainer.h:38: warning: 'int Entry::index'
.../ModelContainer.h:44: warning: when initialized here

Why this? isn't it only initialized on calling the second constructor?
This should be no problem to me so why the warning?

Thanks a lot for your help

Tim
 
H

Howard

silversurfer2025 said:
Hello everyone,
I am using a struct inside another class, which can be used through an
empty constructor or another one. The definition looks like this:

struct Entry{
int index; //line 38
FeatureVector* value; //line 39
int object;
int count;
double variance;
Entry() {}
Entry(FeatureVector* _value, int _index, int _object, int _count,
double _variance)
: value(_value), index(_index), object(_object), count(_count),
variance(_variance) {}
};

When I am compiling I get the following error:

../ModelContainer.h: In constructor 'Entry::Entry(FeatureVector*,
int, int, int, double)':
../ModelContainer.h:39: warning: 'Entry::value' will be initialized
after
../ModelContainer.h:38: warning: 'int Entry::index'
../ModelContainer.h:44: warning: when initialized here

Why this? isn't it only initialized on calling the second constructor?
This should be no problem to me so why the warning?
There's no problem. The warning is just to let you know that you put the
initializer for value before the initializer for index, but because index is
declared before value in the class, it will get initialized first.
Initializations occur in order of their declaration in the class.
(Personally, I'd disable that particular warning, if possible.)

-Howard
 
T

tolgaceylanus

I recommend reordering the initializer list to conform the declaration
order.
This is the recommended practice rather than turning off the compiler
warning in my opinion.

Tolga Ceylan
 
H

Howard

[please don't top-post. re-ordered]
I recommend reordering the initializer list to conform the declaration
order.
This is the recommended practice rather than turning off the compiler
warning in my opinion.

Yeah, that's probably a better option. (Not that it affects the resulting
binaries one way or the other, of course. :))

-Howard
 
S

silversurfer2025

Howard said:
[please don't top-post. re-ordered]
I recommend reordering the initializer list to conform the declaration
order.
This is the recommended practice rather than turning off the compiler
warning in my opinion.

Oh, now I get the error,.. I simply did not think of this as being
warnable at all, but one never stops learning... Switched index and
value and everything is fine now..
Thanks
Tim
 

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
474,146
Messages
2,570,832
Members
47,374
Latest member
anuragag27

Latest Threads

Top