0

我有以下类声明:

class DepthDescriptor
{
public:
    DepthDescriptor(DepthType depth);
    bool operator==(DepthDescriptor& type);
    bool operator>=(DepthDescriptor& type);
    bool operator<=(DepthDescriptor& type);
...
}

为什么以下行不执行到DepthDescriptor对象的隐式转换以便可以进行运算符比较?

if (depth == Depth_8U)
{
...
}

请注意,它depth是一个DepthDescriptor对象,DepthType是一个枚举,并且Depth_8U是枚举值之一。我希望像上面这样的行会首先调用隐式构造函数DepthDescriptor(DepthType depth),然后调用适当的运算符,但我得到no operator "==" matches these operands.

4

2 回答 2

1

尝试

bool operator==(const DepthDescriptor& type) const;
bool operator>=(const DepthDescriptor& type) const;
bool operator<=(const DepthDescriptor& type) const;
于 2012-11-19T09:28:47.873 回答
0

要进行转换,您应该编写全局函数而不是成员函数,并且应该是 const 正确的。IE

bool operator==(const DepthDescriptor& lhs, const DepthDescriptor& rhs)
{
    ...
}

如果您使用成员函数,转换将不会发生在左侧。除非您是 const 正确的,否则转换可能不会在标准的符合编译器中发生。

于 2012-11-19T09:31:34.307 回答