【发布时间】:2015-12-28 03:24:19
【问题描述】:
所以我有这两个类:Foo 和 Bar。
//forward declaration for Bar
class Bar;
typedef enum Type { TYPE1, TYPE2, TYPE3 };
// My class Foo
class Foo
{
public:
explicit Foo(Type fooType)
:mType(fooType)
{}
Type getFooType() const
{
return mType;
}
inline bool operator==(const Bar& bar) { return (bar.getBarType() == mType); } //ERROR: error C2027: use of undefined type 'Bar'
private:
Type mType;
};
// My class Bar
class Bar
{
public:
explicit Bar(Type barType)
:mType(barType)
{}
Type getBarType() const
{
return mType;
}
private:
Type mType;
};
现在,在我的代码中,我想比较两个实例(一个来自 Foo,另一个来自 Bar 类)。 像这样:
if(myBar->getBarType() == myFoo->getFooType())
{
//...
}
问题: 我知道我需要实现 operator== 来允许这种比较。 所以我已经完成了如上所示...... 我得到了那个错误,尽管我已经做出了前向声明。 我在这里缺少什么让我可以在两个类上使用 operator== 进行比较?
【问题讨论】: