0

I am trying to overload the == operator in C++.

#include <string>

using namespace std;

namespace date_independent
{
    class clock
    {
        public:
            int clockPair[2] = {0,0};
            operator string() const
            {
                string hourString;
                string minString;
                if(clockPair[0]<10)
                {
                    hourString = "0"+to_string(clockPair[0]);
                }
                else
                {
                    hourString = to_string(clockPair[0]);
                }
                if(clockPair[1]<10)
                {
                    minString  = "0"+to_string(clockPair[1]);
                }
                else
                {
                    minString = to_string(clockPair[1]);
                }
                return hourString+":"+minString;
            };
            bool operator ==(const clock&clockOne, const clock&clockTwo) const
            {
                return string(clockOne)==string(clockTwo);
            };
    };
};

There is much more code than I have included, but this is the important part. I want it so that the == operator can compare two objects of class clock. E.g., object1==object2. Is there anybody that can help me?

twerk_it_606
  • 63
  • 3
  • 12

4 Answers4

5

A binary operator like == can be overloaded either as a member function with a single parameter (this being the left-hand operand, and the parameter being the right-hand one), or as a non-member function with two parameters for the two operands.

So either

  • move your operator declaration outside the class declaration (moving the definition to a source file, or declaring it inline if you keep the definition in the header); or
  • add friend to the definition, so that it declares a non-member in the surrounding namespace; or
  • remove the first argument from the member function, using this instead.

As a member, it would look like

bool operator==(const const & clockTwo) const {
    return string(*this) == string(clockTwo);
}

You might also want to compare the two integer values directly to save the expense of making strings. You should also remove the rogue ; after the function and namespace definitions, although most modern compilers shouldn't object to their presence.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
1

Your comparison function has been written to take two clock objects and compare them, so it should be a non-member function (after the class definition), without the const qualifier.

bool operator==(const clock& clockOne, const clock& clockTwo)
{
    return string(clockOne) == string(clockTwo);
}

When you have an operator inside the class definition, the left-hand argument is implicitly provided for you (it's *this), so if you wanted to implement it there you'd need something like:

bool operator==(const clock& clockTwo) const
{
    return string(*this) == string(clockTwo);
}

Still, that's not recommended for == as if you have say an implicit constructor from another type T, you won't be able to write code ala my_t == my_clock with the member version unless T provides a suitable comparison operator (for clock or string). A non-member operator gives more symmetric operation.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252
1

Though your question is poorly worded I believe that you are asking why the operator you've defined is not working?

If you are defining the operator as a member of the class it only takes one parameter. For example:

class clock {
    bool operator ==(const clock& rhsClock) const
    {
        // Note: this is the lhsClock
        return string(*this) == string(otherClock);
    }
};

When you define the operator as a free function (not as a part of the class) then you need to define both parameters:

class clock {
    // ... class declaration ...
};

bool operator ==(const clock& lhsClock, const clock& rhsClock)
{
    return string(lhsClock) == string(rhsClock)
}

Where the comparison would look like this:

if (lhsClock == rhsClock) // ... do something ...
nonsensickle
  • 4,438
  • 2
  • 34
  • 61
1

Overloading can be done inside or outside the class definition. If you want to do it inside, the function receives only one argument. You should compare this with that argument.

bool operator ==(const clock&clockTwo) const
{
    return string(*this)==string(clockTwo);
}

Note the const after the argument, it means that you won't change this inside the function. On the other hand, if you want to do it outside the class definition, it needs two arguments, and you should compare them.

bool operator ==(const clock&clockOne, const clock&clockTwo)
{
    return string(clockOne)==string(clockTwo);
}

Also note that it'll be faster to compare the clockPair of the objects rather than making the string and comparing them.

MartínV
  • 185
  • 1
  • 5