Why do I get these warnings in Visual C ++ 2008 when building a structure?

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 
+6
source share
3 answers

Warning C4610 is incorrect. This is a known bug in Visual C ++. See Microsoft Connect Error Incorrect C4610 Issue.

Adam Rosenfield explains why two other warnings are issued (C4510 and C4512).

+7
source

This is what the compiler says: it cannot create a default constructor or assignment operator for your structure, since it has a const member ( const bool fooBool ). structure elements that are const or that are references cannot be initialized by default, so they must be explicitly initialized in a user-written constructor or assignment statement.

One solution is to write your own default constructor and assignment operator (and according to the rule of the three options , you should also write a copy constructor, the destructor isn’t strictly necessary, but this is good practice). An alternative, simpler solution is to simply make fooBool not const . Then the compiler will happily generate a default constructor and assignment operator for you.

Since you are already creating an array from const instances with static const fooStruct foo[] = ... , the extra const on fooBool pointless.

+10
source

Also, if you are doing partial initialization, then MSVC2008 will cause errors (like MSVC2010), which is incorrect behavior, as defined by C ++ 03 and C ++ 11. I added more about this in another thread, which you can read here

 // Partial initialization, leaving it to the compiler // to do aggregate value-initialization fooStruct foo ={"file1", /*missing true/false, compiler should set false*/ }; 

MSVC throws an error in this code along with the warnings you mentioned.

0
source

Source: https://habr.com/ru/post/895617/


All Articles