lundi 12 octobre 2020

Compile error initializing struct member with enum class

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

Aucun commentaire:

Enregistrer un commentaire