所以,这是我的问题:我想创建一个类,然后根据其参数将其与==进行比较以比较内部枚举值。所以这是我尝试的:
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'|
而且...我不知道。有任何想法吗 ?
最佳答案
进行以下一行:
bool operator==(T_values& value) {return (value == m_value);}
到这个:
// v-- no reference
bool operator==(T_values value) {return (value == m_value);}
您不能通过引用获取文字
Type::VALUE
,因为它不是对象。关于c++ - 没有匹配运算符==的2D vector ,枚举和指针,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27448908/