std::string:将此类型非法用作表达式
std::string : Illegal use of this type as an expression
#include <functional>
#include <string>
enum MaybeType{
Nothing,
Just
};
template<typename T>
class Maybe{
virtual MaybeType getType() const = 0;
};
template<typename T>
class Just : public Maybe<T>{
T value;
virtual MaybeType getType() const{
return MaybeType::Just;
}
public:
Just(T v) : value(v){}
};
template<typename T>
class Nothing : public Maybe<T>{
virtual MaybeType getType() const{
return MaybeType::Nothing;
}
};
int main(){
using namespace std;
string s = "Hello";
auto m = Just<string>(s); // error
}
我得到以下错误"std::string"error C2275: 'std::string' : illegal use of this type as an expression
为什么我会出现这个错误,在这种情况下它意味着什么?
问题是您的代码为Nothing
和Just
提供了两种含义:
- 枚举中的值,以及
- 模板类型
编译器似乎更喜欢前者;你想要晚一点。
为了解决这个问题,你可以做三件事之一:
- 重命名您的
enum
值 - 重命名模板类,或者
- 请确保两个冲突的名称属于不同的名称空间
使用重命名的enum
常量在ideone上演示。
在ideone上演示enum
的独立命名空间。
相关文章:
- (C++)分析树以计算返回错误值的简单算术表达式
- 无法编译 rtmidi 测试 cmidiin.cpp 文件, 非法指令
- "class":非法使用此类型作为表达式 如何解决?
- 在 C++ 中的 lambda 表达式中非法捕获
- 调用模板类成员时非法使用此类型作为表达式
- C2276 '&':对绑定成员函数表达式的非法操作
- 排序测试模板化函数 lambda:非法使用此类型作为表达式
- 错误 C2275 RHandle:非法使用此类型作为表达式
- '&' :对绑定成员函数表达式的非法操作
- 错误:非法使用此类型作为表达式.C++
- LAMBDA 错误:类型 'void' 的条件表达式是非法的
- 为什么double v2=((double)51/4)%2是非法表达式ni c++
- "&":对绑定成员函数表达式的非法操作。从关键点矢量 OpenCV 获取容量
- 非法使用type作为表达式
- 无法在VS 14 CTP中使用auto声明lambda:类型为'void'的条件表达式是非法的
- std::string:将此类型非法用作表达式
- "!"绑定成员函数表达式的非法操作
- 奇怪的错误 C2275 ..非法使用此类型作为成员函数模板和 lambda 的表达式
- 非法使用此类型作为表达式(错误)c++
- 非法使用此类型作为表达式- VS 2012