C++ 使用枚举表示从“int”到“type”的无效转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8703743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 19:01:06  来源:igfitidea点击:

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

c++enums

提问by mahmood

In my class I defined an enum like this:

在我的课堂上,我定义了一个这样的枚举:

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

Later in defined an object like this:

后来定义了一个这样的对象:

myClass ob;
ob->aType = 0;

However I get this error:

但是我收到这个错误:

error: invalid conversion from 'int' to 'myClass::AccessType {aka myClass::access}' [-fpermissive]

Don't enum fields map to integers?

不要枚举字段映射到整数吗?

采纳答案by ybungalobill

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.

从 enum 到 int 的转换是隐式的。

回答by bobbymcr

Enumeration members are backed by integer values but there is no implicitconversion from an integer to an enum type. You need to use an explicit cast if you really want to write it like this:

枚举成员由整数值支持,但没有从整数到枚举类型的隐式转换。如果你真的想这样写,你需要使用显式转换:

ob->aType = static_cast<myClass::access>(0);

回答by rejj

You can't do an implicit cast from int -> enum, since at compile time there is no way to know that the cast is valid.

您不能从 int -> enum 进行隐式转换,因为在编译时无法知道转换是否有效。

You cando implicit casts the other way, so you could (if you wanted to) do:

可以用另一种方式进行隐式转换,所以你可以(如果你想)这样做:

int foo = forL;

回答by Pellekrino

I just had same issue. i have to initialize an object from what i read in an xml file and for sure, i have no control over what could happen to that file.

我只是有同样的问题。我必须从我在 xml 文件中读取的内容初始化一个对象,当然,我无法控制该文件可能发生的情况。

Constructor has an enum as argument :

构造函数有一个枚举作为参数:

enum status_t { NOT_STARTED, STARTED, DONE };
MyObject::MyObject(int id, status_t status) : m_id(id), m_status(status){}

So when parsing the Xml i have to cast it. I prefered then to handle the cast in the constructor so the other classes do not have to know which is the valid enums.

所以在解析 Xml 时我必须转换它。我更喜欢在构造函数中处理强制转换,这样其他类就不必知道哪个是有效的枚举。

MyObject::MyObject(int id, int status) : m_id(id){
    m_status = status_t(status);
}

But no way to be sure the value coming from xml will be in the correct range.

但是无法确保来自 xml 的值在正确的范围内。

Here is the solution i came with :

这是我带来的解决方案:

MyObject::MyObject(int id, int status) : m_id(id){
    switch(status){
        case NOT_STARTED:
        case STARTED:
        case DONE:
            m_status=status_t(status);
            break;
        default:
            m_status=NOT_STARTED;
            break;
    }
}

This is an implementation choice, to force a default value in case of non consistent data. One could prefer throwing out an exception, in my case it will do this way.

这是一种实现选择,用于在数据不一致的情况下强制使用默认值。人们可能更喜欢抛出异常,在我的情况下它会这样做。