C++ C++;被迫做一个奇怪的演员来摆脱;表达式应为可修改的左值“;

C++ C++;被迫做一个奇怪的演员来摆脱;表达式应为可修改的左值“;,c++,enums,casting,C++,Enums,Casting,我刚刚遇到了一个奇怪的枚举分配问题,我想你可以帮我解决这个问题。我有这样一个枚举: enum LIB_EDXENGINE CameraFeatureDataType { CFDT_ENUMERATION, CFDT_64BITS_UINT, CFDT_64BITS_INT, CFDT_64BITS_FLOAT, CFDT_BOOLEAN, CFDT_32BITS_UINT, C

我刚刚遇到了一个奇怪的枚举分配问题,我想你可以帮我解决这个问题。我有这样一个枚举:

enum LIB_EDXENGINE CameraFeatureDataType
    {
        CFDT_ENUMERATION,
        CFDT_64BITS_UINT,
        CFDT_64BITS_INT,
        CFDT_64BITS_FLOAT,
        CFDT_BOOLEAN,
        CFDT_32BITS_UINT,
        CFDT_32BITS_INT,
        CFDT_32BITS_FLOAT,
    };  
我有一个类,它有这个枚举的一个实例。
编辑:

const CameraFeatureDataType & LibEDX::CVBGenicamFeature2::GetFeatureType() const
{
    switch (m_eNodeType)
    {
    case NT_Boolean:
        m_eCameraFeatureDataType = CFDT_BOOLEAN;
        break;
    case NT_Integer:
        m_eCameraFeatureDataType = CFDT_64BITS_INT;
        break;
    case NT_Float:
        m_eCameraFeatureDataType = CFDT_64BITS_FLOAT;
        break;
    case NT_Enumeration:
        m_eCameraFeatureDataType = CFDT_ENUMERATION;
        break;
    }
    return m_eCameraFeatureDataType;
}
const CameraFeatureDataType & LibEDX::CVBGenicamFeature2::GetFeatureType() const
{
    switch (m_eNodeType)
    {
    case NT_Boolean:
        (CameraFeatureDataType)m_eCameraFeatureDataType = CFDT_BOOLEAN;
        break;
    case NT_Integer:
        (CameraFeatureDataType)m_eCameraFeatureDataType = CFDT_64BITS_INT;
        break;
    case NT_Float:
        (CameraFeatureDataType)m_eCameraFeatureDataType = CFDT_64BITS_FLOAT;
        break;
    case NT_Enumeration:
        (CameraFeatureDataType)m_eCameraFeatureDataType = CFDT_ENUMERATION;
        break;
    }
    return m_eCameraFeatureDataType;
}
我发现消除错误的唯一解决方案是将成员变量强制转换为它自己的类型,我认为这很奇怪。
也已编辑:

const CameraFeatureDataType & LibEDX::CVBGenicamFeature2::GetFeatureType() const
{
    switch (m_eNodeType)
    {
    case NT_Boolean:
        m_eCameraFeatureDataType = CFDT_BOOLEAN;
        break;
    case NT_Integer:
        m_eCameraFeatureDataType = CFDT_64BITS_INT;
        break;
    case NT_Float:
        m_eCameraFeatureDataType = CFDT_64BITS_FLOAT;
        break;
    case NT_Enumeration:
        m_eCameraFeatureDataType = CFDT_ENUMERATION;
        break;
    }
    return m_eCameraFeatureDataType;
}
const CameraFeatureDataType & LibEDX::CVBGenicamFeature2::GetFeatureType() const
{
    switch (m_eNodeType)
    {
    case NT_Boolean:
        (CameraFeatureDataType)m_eCameraFeatureDataType = CFDT_BOOLEAN;
        break;
    case NT_Integer:
        (CameraFeatureDataType)m_eCameraFeatureDataType = CFDT_64BITS_INT;
        break;
    case NT_Float:
        (CameraFeatureDataType)m_eCameraFeatureDataType = CFDT_64BITS_FLOAT;
        break;
    case NT_Enumeration:
        (CameraFeatureDataType)m_eCameraFeatureDataType = CFDT_ENUMERATION;
        break;
    }
    return m_eCameraFeatureDataType;
}

将方法声明为const,然后修改成员属性:

const CameraFeatureDataType & LibEDX::CVBGenicamFeature2::GetFeatureType() const
{
switch (m_eNodeType)
{
case NT_Boolean:
    this->m_eCameraFeatureDataType = CFDT_BOOLEAN;
    break;
case NT_Integer:
    this->m_eCameraFeatureDataType = CFDT_64BITS_INT;
    break;
case NT_Float:
    this->m_eCameraFeatureDataType = CFDT_64BITS_FLOAT;
    break;
case NT_Enumeration:
    this->m_eCameraFeatureDataType = CFDT_ENUMERATION;
    break;
}
return this->m_eCameraFeatureDataType;
}
你应该写:

const CameraFeatureDataType & LibEDX::CVBGenicamFeature2::GetFeatureType() const
{
switch (m_eNodeType)
{
case NT_Boolean:
    return CFDT_BOOLEAN;
case NT_Integer:
    return CFDT_64BITS_INT;
case NT_Float:
    return CFDT_64BITS_FLOAT;
case NT_Enumeration:
    return CFDT_ENUMERATION;
}
return CFDT_UNKNOWN;//change to default value
}

将方法声明为const,然后修改成员属性:

const CameraFeatureDataType & LibEDX::CVBGenicamFeature2::GetFeatureType() const
{
switch (m_eNodeType)
{
case NT_Boolean:
    this->m_eCameraFeatureDataType = CFDT_BOOLEAN;
    break;
case NT_Integer:
    this->m_eCameraFeatureDataType = CFDT_64BITS_INT;
    break;
case NT_Float:
    this->m_eCameraFeatureDataType = CFDT_64BITS_FLOAT;
    break;
case NT_Enumeration:
    this->m_eCameraFeatureDataType = CFDT_ENUMERATION;
    break;
}
return this->m_eCameraFeatureDataType;
}
你应该写:

const CameraFeatureDataType & LibEDX::CVBGenicamFeature2::GetFeatureType() const
{
switch (m_eNodeType)
{
case NT_Boolean:
    return CFDT_BOOLEAN;
case NT_Integer:
    return CFDT_64BITS_INT;
case NT_Float:
    return CFDT_64BITS_FLOAT;
case NT_Enumeration:
    return CFDT_ENUMERATION;
}
return CFDT_UNKNOWN;//change to default value
}

您的问题是试图修改
const
方法中的成员变量

这是您的方法通常的样子:

CameraFeatureDataType LibEDX::CVBGenicamFeature2::GetFeatureType() const
{
    switch (m_eNodeType)
    {
    case NT_Boolean:
        return CFDT_BOOLEAN;
    case NT_Integer:
        return CFDT_64BITS_INT;
    case NT_Float:
        return CFDT_64BITS_FLOAT;
    case NT_Enumeration:
        return CFDT_ENUMERATION;
    default:
        // don't know either, you decide...
    }
}

我不知道为什么会有一个成员变量,或者为什么会返回一个枚举值作为常量引用。如果你需要其中一个,你应该仔细想想为什么你需要它们。在
const
方法中更改成员变量的关键字是
mutable

您的问题是试图修改
const
方法中的成员变量

这是您的方法通常的样子:

CameraFeatureDataType LibEDX::CVBGenicamFeature2::GetFeatureType() const
{
    switch (m_eNodeType)
    {
    case NT_Boolean:
        return CFDT_BOOLEAN;
    case NT_Integer:
        return CFDT_64BITS_INT;
    case NT_Float:
        return CFDT_64BITS_FLOAT;
    case NT_Enumeration:
        return CFDT_ENUMERATION;
    default:
        // don't know either, you decide...
    }
}

我不知道为什么会有一个成员变量,或者为什么会返回一个枚举值作为常量引用。如果你需要其中一个,你应该仔细想想为什么你需要它们。在
const
方法中更改成员变量的关键字是
mutable

太好了,所以你脑子里有一个特别的错误。你能发一封邮件让我们复制一下吗?您的代码片段没有泄露任何信息。无论如何,您缺少了几个
break
语句。switch语句可能位于
const
-限定成员函数中吗?@BoPersson-很好!但是对一个值的转换本身就是一个问题,不是吗?那么问题是,为什么它是一个引用?你希望打电话的人改变它吗?如果您必须在常量函数中修改它,请使其可变。太好了,所以您心里有一个特定的错误。你能发一封邮件让我们复制一下吗?您的代码片段没有泄露任何信息。无论如何,您缺少了几个
break
语句。switch语句可能位于
const
-限定成员函数中吗?@BoPersson-很好!但是对一个值的转换本身就是一个问题,不是吗?那么问题是,为什么它是一个引用?你希望打电话的人改变它吗?如果你必须在常量函数中修改它,那就让它变为可变的。它成功了!像这样的东西一开始就在那里。但是我丢失了开关外部的返回值,我认为编译器发出了一些警告,它们现在不见了。谢谢顺便说一句,引用对我来说就像是一个变量的“链接”,从方法中访问它。例如,我不理解如何将CFDT_BOOLEAN视为参考。枚举不同于经典变量,这就是为什么我比引用std::string(例如实例化)更困惑的原因。它成功了!像这样的东西一开始就在那里。但是我丢失了开关外部的返回值,我认为编译器发出了一些警告,它们现在不见了。谢谢顺便说一句,引用对我来说就像是一个变量的“链接”,从方法中访问它。例如,我不理解如何将CFDT_BOOLEAN视为参考。枚举不同于经典变量,这就是为什么我比引用std::string(例如实例化)更困惑的原因。