Following program giving compilation error as following
// Example program
#include <iostream>
#include <string>
enum class Animation: int{
Hide=0,
Show,
Flicker
};
struct Icon {
int id;
char name[10];
Animation currentAnim;
Animation nextAnim;
int isActive;
};
static struct Icon IconList[]= {
{1, "Offline", Animation::Hide, Animation::Hide, 1},
{2, "Training", Animation::Hide, Animation::Hide, 1},
{0, 0, Animation::Hide, Animation::Hide, 1}
};
int main()
{
std::cout << "Doesn't matter";
}
Compilation
23:1: error: cannot convert 'Animation' to 'char' in initialization 23:1: error: cannot convert 'Animation' to 'char' in initialization
If I change the last member of IconsList[] to this, the compilation error is fixed.
{0, "", Animation::Hide, Animation::Hide, 1}
Can you explain the reason? Why I am getting such a compilation error message for the case?
If I use int instead of enum class, I don't face this compilation error
The braces around the nested initializer lists may be omitted in aggregate initialization,
The member
nameis a subaggregate containing 10 elements, the 2nd0in the initializer is just used to initialize the 1st element ofname, thenAnimation::Hideis tried to use to initialize the 2nd and the 3rd element; butAnimationcan't convert tocharimplicitly (as a scoped enumeration).That's because
intcould convert tocharimplicitly. Note that for this case some of the members ofIconmight be left uninitialized.You can add braces for nested initializer if your intent is to use
0to initialize the membername; as the effect the 1st element ofnameis initialized as0, and all the remaining elements are value-initialized (zero-initialized) to0too, juse same as initializing it with"".