I have this code
typedef struct
{
const char* fooString;
const bool fooBool;
}fooStruct;
And this initializer:
static const fooStruct foo[] =
{
{"file1", true},
{"file2", false},
....
};
With this code I have 3 warnings in VS2008:
error C2220: warning treated as error - no 'object' file generated
warning C4510: '<unnamed-tag>' : default constructor could not be generated
warning C4512: '<unnamed-tag>' : assignment operator could not be generated
warning C4610: struct '<unnamed-tag>' can never be instantiated - user defined constructor required
It's exactly what the compiler says: it can't generate a default constructor or assignment operator for your struct because it has a const
member in it (const bool fooBool
). struct members which are const
or which are references cannot be default-initialized, so they must be explicitly initialized in a user-written constructor or assignment operator.
One solution is to write your own default constructor and assignment operator (and in line with the rule of three, you should also write a copy constructor; a destructor isn't strictly necessary but is good practice). The alternative, easier solution is just to make fooBool
non-const
. Then, the compiler will happily generate the default constructor and assignment operator for you.
Since you're already creating an array of const
instances of these with static const fooStruct foo[] = ...
, the extra const
on fooBool
is pointless.
The C4610 warning is incorrect. This is a known bug in Visual C++. See the Microsoft Connect bug "Improper issuance of C4610."
Adam Rosenfield explains why the other two warnings (C4510 and C4512) are emitted.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With