using enum says invalid conversion from 'int' to 'type'

后端 未结 4 1646
隐瞒了意图╮
隐瞒了意图╮ 2021-02-05 02:51

In my class I defined an enum like this:

class myClass 
{
 public:
    enum access {
      forL,
      forM,
      forA
    };
    typedef access AccessType;
            


        
4条回答
  •  抹茶落季
    2021-02-05 03:16

    No, they are stored as integers but they are distinct types (e.g. you can even overload based on the enum type). You must convert explicitly:

    myClass ob;
    ob->aType = (myClass::AccessType)0;
    

    or ever better write the corresponding named value of the enum:

    myClass ob;
    ob->aType = myClass::forL;
    

    Or perhaps if you want to use the enum just as a set of integer constants, change the type of the field:

    class myClass 
    {
     public:
        enum {
          forL,
          forM,
          forA
        };
        int aType; // just stores numbers
    };
    

    Conversion from enum to int is implicit.

提交回复
热议问题