Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/151.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 运算符==比较两个不同的类_C++_Operator Overloading - Fatal编程技术网

C++ 运算符==比较两个不同的类

C++ 运算符==比较两个不同的类,c++,operator-overloading,C++,Operator Overloading,所以我有两个类: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 { ret

所以我有两个类:
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())
{
 //...
}
问题: 我知道我需要实现操作符==来允许这种比较。 所以我做了如上所述的。。。 我得到了这个错误,尽管我已经做了远期声明。
在这里我缺少什么可以让我在两个类上使用操作符==进行比较?

您需要在
类定义之后定义
操作符==
,而不是在
Foo
类中。在类中声明它,但在外部定义它

inline Foo::operator==(const Bar &bar) const { ... }
这对上面的测试没有多大帮助,因为左侧有
Bar
元素,右侧有
Foo
,因此不会考虑Foo中的
操作符==
。定义符号全局
运算符==
函数将非常有用

inline bool operator==(const Bar &bar, const Foo &foo) { return foo == bar; }

你说你想让这种比较起作用

if(myBar->getBarType() == myFoo->getFooType())
这是比较
getXType
函数返回的
enum Type
值,而不是
Foo
Bar
对象本身,默认情况下,枚举是可比较的,因此您不需要提供自己的
操作符==

尽管如此,你已经试着这么做了,而且

inline bool operator==(const Bar& bar) { return (bar.getBarType() == mType); } //ERROR: error C2027: use of undefined type 'Bar'
…问题是在翻译单元中出现此函数定义的位置未定义
条形图
。您可以只声明函数

inline bool operator==(const Bar& 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
相比,这仍然只允许您省略显式的
getFooType()
调用。要支持您请求的其他排序,您需要在
条中使用类似的运算符,该运算符与
常量Foo&
一起工作

还有一个细节。。。你说

我得到了这个错误,尽管我已经做了远期声明


转发声明只是让编译器知道
Bar
是一个类,它不会告诉编译器
Bar
包含
Type getBarType()
成员函数,编译
操作符==
需要这些知识,谢谢。但是,正如您所建议的那样,使用symetric全局运算符==时,我得到了一个错误:二进制“operator==”的参数太多。如何用2个参数重载运算符==?@waas1919将其置于任何类之外