所以,这就是我的问题:根据其参数,我想创建一个类,而不是与==内部枚举值进行比较。所以这就是我的尝试:
class Type
{
public:
enum T_values {VALUE,
OTHERVALUE
};
Type(T_values value) : m_value(value) {}
bool operator==(T_values& value) {return (value == m_value);}
private:
T_values m_value;
};
struct foo
{
foo(Type TYPE) : m_Ts(1, std::vector<Type*>(1, &TYPE)) {}
std::vector<std::vector<Type*>> m_Ts;
void bar(int, int);
};
void foo::bar(int i, int j)
{
if(*m_Ts[i][j] == Type::VALUE)
{ cout<<"it works"; }
}
int main()
{
Type TYPE(Type::VALUE);
foo test(TYPE);
test.bar(0,0);
return 0;
}
然后,我有一个漂亮而明确的编译错误:
...\workspace\main.cpp|29|error: no match for 'operator==' in '*(&((foo*)this)->foo::m_Ts.std::vector<_Tp, _Alloc>::operator[]<std::vector<Type*>, std::allocator<std::vector<Type*> > >(((std::vector<std::vector<Type*> >::size_type)i)))->std::vector<_Tp, _Alloc>::operator[]<Type*, std::allocator<Type*> >(((std::vector<Type*>::size_type)j)) == (Type::T_values)0u'|
而且......我不知道。有什么想法吗?
答案 0 :(得分:2)
建立这一行:
bool operator==(T_values& value) {return (value == m_value);}
进入这个:
// v-- no reference
bool operator==(T_values value) {return (value == m_value);}
您不能通过引用获取文字Type::VALUE
,因为它不是对象。
答案 1 :(得分:1)
这条线也有问题。
foo(Type TYPE) : m_Ts(1, std::vector<Type*>(1, &TYPE)) {}
您正在存储临时对象的地址。函数返回时该地址无效。