#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
}
我收到以下错误&#39; std :: string&#39; error C2275: 'std::string' : illegal use of this type as an expression
为什么我会收到此错误,在这种情况下它意味着什么?
答案 0 :(得分:2)
问题是您的代码为Nothing
和Just
提供了两种含义:
编译器似乎更喜欢前者;你想要晚一点。
为了解决这个问题,你可以做以下三件事之一:
enum
值,