C++ 被迫做一个奇怪的演员表来摆脱"expression should be a modifiable lvalue"

C++ Forced to do a weird cast to get rid of "expression should be a modifiable lvalue"

本文关键字:expression should lvalue modifiable be 被迫 一个 C++      更新时间:2023-10-16

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

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,然后执行成员属性修改:

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

相关文章: