0

I am a beginner in programming and I am learning how to use the operator== When using it in Visual Studio I ran into an issue, I get the following error :

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  

2 Answers2

2

In Duree.h you've included operator== inside the class, while what you probably meant was to make it a friend function of this class.

Add the keyword friend before the operator== declaration in Duree.h, and it should work.

Benjamin Buch
  • 4,752
  • 7
  • 28
  • 51
Mike Tyukanov
  • 579
  • 4
  • 10
  • Even better: just move the declaration of `operator==` outside the class definition. It doesn't need to be a friend. +1. – Pete Becker Jun 24 '23 at 12:13
  • @PeteBecker, agree. All the access to private variables is in estEgal public method, to it's possible to simply delete the declaration of operator== from class. But I don't know the intent of this snippet, it's obviously an exercise and who knows what the next step will be. I sometimes start with similar simple things to show students how "friend" can change when we make a template out of a class. – Mike Tyukanov Jun 24 '23 at 12:21
  • Thanks my friend it's working now – عبدالرحمان سوديني Jun 24 '23 at 12:41
  • IMO it is better to have the operator as friend without a declaration outside of the class. This way the operator is only found through ADL and does not pollute the global namespace. – j6t Jun 24 '23 at 15:18
1

If you define the operator as non-static member function, there is one additional "hidden parameter": this.

For this reason operators implemented as non-static member functions must take only a single parameter: The second operand of the operator.

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);

You could also add the implement an operator at namespace scope inside the class body by making the operator a 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
fabian
  • 80,457
  • 12
  • 86
  • 114
  • In the code in the question, `operator==` is implemented by calling `estEgal` (which, I assume, means something like "is equal"). Given that, there's no need for the two-argument version to be a friend, since `estEgal` is a public member. Just declare it outside the class. – Pete Becker Jun 24 '23 at 12:17
  • @PeteBecker The purpose of that part of the answer wasn't to consider, whether making the function a friend is actually necessary, but to demonstrate the use of putting `friend ` inside the definition of a class. – fabian Jun 24 '23 at 12:20
  • @fabian Thanks my friend it's working now – عبدالرحمان سوديني Jun 24 '23 at 12:51