c++ C2804二进制“operator ==”参数太多[重复]

bqujaahr  于 2023-06-25  发布在  其他
关注(0)|答案(2)|浏览(85)

此问题已在此处有答案

What are the basic rules and idioms for operator overloading?(8个回答)
20小时前关闭。
我是一个编程初学者,我正在学习如何使用operator==当在Visual Studio中使用它时,我遇到了一个问题,我得到了以下错误:

C2804   binary 'operator ==' has too many parameters

Duree.cpp

#include "Duree.h"
#include<iostream>
Duree::Duree(int h,int m,int s) :
    m_h(h),m_m(m),m_s(s)
{

}

bool Duree::estEgal(Duree const& b) const {
    return (m_h == b.m_h && m_m == b.m_m && m_s == b.m_s);

}
bool operator==(Duree const& a, Duree const& b)
{
    return a.estEgal(b);
}

Duree.h

#ifndef DUREE_H_INCLUDED
#define DUREE_H_INCLUDED

class Duree
{
public:
    Duree(int h = 0,int m = 0,int s = 0);
 
    bool estEgal(Duree const& b) const;

    bool operator==(Duree const& a, Duree const& b);

private:
    int m_h;
    int m_m;
    int m_s;
};

#endif

main.cpp

#include <iostream>
#include "Duree.h"

using namespace std;

int main()
{
    Duree a(10,10,10),b(20,20,20);

    cout << "Hello world! " << endl;
    if (a == b) {
        cout << "a = b";
    }
    

    return 0;
}

Error:

Error   C2804   binary 'operator ==' has too many parameters            
Error   C2676   binary '==': 'Duree' does not define this operator or a conversion to a type acceptable to the predefined operator  cplusplus   
Error   C2804   binary 'operator ==' has too many parameters    cplusplus       
Error (active)  E0349   no operator "==" matches these operands cplusplus   


Severity    Code    Description Project File    Line    Suppression State
Error   C2804   binary 'operator ==' has too many parameters    cplusplus   C:\Users\ACER\source\repos\cplusplus\cplusplus\Duree.h  12  
Error   C2676   binary '==': 'Duree' does not define this operator or a conversion to a type acceptable to the predefined operator  cplusplus   C:\Users\ACER\source\repos\cplusplus\cplusplus\cplusplus.cpp    11  
Error   C2804   binary 'operator ==' has too many parameters    cplusplus   C:\Users\ACER\source\repos\cplusplus\cplusplus\Duree.h  12  
Error (active)  E0349   no operator "==" matches these operands cplusplus   C:\Users\ACER\source\repos\cplusplus\cplusplus\cplusplus.cpp    11
klr1opcd

klr1opcd1#

Duree.h中,你已经在类中包含了operator==,而你的意思可能是让它成为这个类的friend函数。
Duree.h中的operator==声明之前添加关键字friend,它应该可以工作。

3qpi33ja

3qpi33ja2#

如果将运算符定义为非静态成员函数,则会有一个额外的“隐藏参数”:this
因此,作为非静态成员函数实现的运算符必须只接受一个参数:运算符的第二个操作数。

class Duree
{
public:
    Duree(int h = 0, int m = 0, int s = 0);

    bool operator==(Duree const& b) const
    {
        return (m_h == b.m_h && m_m == b.m_m && m_s == b.m_s);
    }

private:
    int m_h;
    int m_m;
    int m_s;
};

...

Duree a;
Duree b;

//the following 2 rhs expressions are the same
bool b1 = (a == b); 
bool b2 = a.operator==(b);

你也可以在类主体的命名空间范围内添加implemental操作符,方法是将操作符设为friend

class Duree
{
public:
    Duree(int h = 0, int m = 0, int s = 0);

    friend bool operator==(Duree const& a, Duree const& b)
    {
        return (a.m_h == b.m_h && a.m_m == b.m_m && a.m_s == b.m_s);
    }

private:
    int m_h;
    int m_m;
    int m_s;
};

// the operator definition above basically has the same effect as defining
//
// inline bool operator==(Duree const& a, Duree const& b)
//
// here + adding it as a friend of the Duree class

相关问题