C++ 被迫做一个奇怪的演员表来摆脱"expression should be a modifiable lvalue"
C++ Forced to do a weird cast to get rid of "expression should be a modifiable lvalue"
我刚刚遇到了一个奇怪的枚举分配问题,我想你可以帮助我。我有一个这样的枚举:
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
。
相关文章:
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- C++核心准则 C35 对于接口类"A base class destructor should be either public and virtual, or protected and nonv
- 为什么static_assert错误:即使我传递常量"expression must have a constant value"?
- C++ "error: invalid use of void expression"
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 对象声明中的模板"error: expected expression"
- 为什么我的功能在使用 goto 时会给我带来"expected primary-expression before '}' token"?
- 为什么我会"error: expected expression"?
- 叮当声:折叠表情和"expression result unused"警告
- 错误消息"expected expression"....有人知道它为什么这么说吗?
- C++ 向量加减抛出"expression: vector subscript out of range."错误
- "expression must be a modifiable lvalue"是什么意思?
- 如何理解"Temporary objs are destroyed as the last step in evaluating the full-expression"?谁能用一些简单的例子来说明这
- "noexcept-expression 由于对......的调用而计算为'假'"是什么意思?
- unordered_set .find 方法无法编译的 C++ "expression must have class type"错误
- 如何修复此错误"expected primary expression before 'int'"?
- 使用结构和数组时的"expected primary expression before '.' token"
- 如何修复 eigen3 中的'non-type template argument is not a constant expression'?
- 编译为 cuda 内核调用提供了"expression must have integral or unscoped enum type"
- C++ 被迫做一个奇怪的演员表来摆脱"expression should be a modifiable lvalue"