Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9151503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:49:07+00:00 2026-06-17T11:49:07+00:00

I have defined a class like this using namespace std; class foo { public:

  • 0

I have defined a class like this

using namespace std;
class foo {
public:
  typedef std::pair< int, int > index;
  bool operator == ( const index &l, const index &r )
  {
    return (l.first == r.first && l.second == r.second);
  }
  void bar()
  {
    index i1;
    i1.first = 10;
    i1.second = 20;
    index i2;
    i2.first = 10;
    i2.second = 200;
    if (i1 == i2)
       cout << "equal\n";
  }
};

However I get this error in windows

error C2804: binary 'operator ==' has too many parameters

and this error in linux

operator==(const  std::pair<int, int>&, const std::pair<int, int>&)’ must take exactly one argument

I found this topic overloading operator== complaining of 'must take exactly one argument' and seems to be a problem with static and non-static functions in a class. However I don’t know how to apply this

For example, this is not correct

  bool operator == ( const index &r )
  {
    return (this->first == r.first && this->second == r.second);
  }

How can I fix that?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T11:49:08+00:00Added an answer on June 17, 2026 at 11:49 am

    The operator== can be implemented in two ways:

    • As member function: in this case, the function takes one argument and is invoked on the left operand which is passed as this pointer implicitly to the function.
    • As non-member function, in which case, the function takes two arguments, left and right operands.

    Since you’re implementing operator== for std::pair, you cannot implement it as member function (of std::pair). The option you’re left with is non-member function.

    So implement it outside the class as:

    bool operator==(std::pair<int,int> const & l, std::pair<int,int> const & r)
    {
        return (l.first == r.first && l.second == r.second);
    }
    

    But then you don’t really need to implement it yourself unless you want to implement it differently. The Standard Library has already provided a generic version of operator== for std::pair which lexicographically compares the values in the pair, like I did above, i.e compare first with first and second with second. If you need to compare them differently, only then provide your own specific definition (non-template version).

    The above mentioned points are worth noting as to how to implement operator== when you need it for your defined types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a function defined like this: class Foo() { public: void bar(MyClass*
I have this code: #include <iostream> using namespace std; class FooA { public: virtual
I have a Customer class defined like this: @Entity public class Customer { //...
I have a ViewModel defined like this: public class LocationTreeViewModel<TTree> : ObservableCollection<TTree>, INotifyPropertyChanged TTree
I have a class defined like this, with the appropriate getter and setter methods...
We have a class Car defined like this in a car.rb file class Car
I have a declarative table defined like this: class Transaction(Base): __tablename__ = transactions id
I have a class called MainGame, which is defined like this in my .h:
I have a class that defines its own enum like this: public class Test
#include <iostream> using namespace std; template <typename T> class test { T y; public:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.