Jacob Voronin
Jacob Voronin

Reputation: 25

C++ == operator overloading (impementations)

I have these prototypes declared in ULong.h

  bool operator== (const ULong& ) const;
  bool operator== (unsigned long long) const;
  friend bool operator== (unsigned long long, const ULong&);

In ULong.cpp, I'm trying to implement them:

bool ULong::operator== (const ULong& ul) const
{
    if(_num_digits != ul._num_digits)
        return false;
    for(unsigned i = 0;i < _num_digits; i++)
    {
        if(_number[i] != ul._number[i])
            return false;
    }
    return true;
}

bool ULong::operator== (unsigned long long l) const
{
    return *this == ULong(l);
}

ULong operator== (unsigned long long l, const ULong& ul)
{
    return ULong(l) == ul;
}

And I get compiler error:

ULong.cpp:358:56: error: new declaration ‘ULong operator==(long long unsigned int, const ULong&)’ In file included from ULong.cpp:10:0:

ULong.h:76:15: error: ambiguates old declaration ‘bool operator==(long long unsigned int, const ULong&)’

I can't understand how to implement this method properly.

Upvotes: 0

Views: 228

Answers (3)

BlackCat
BlackCat

Reputation: 183

The return type is the problem. In the definition its ULong and it should be bool. ;)

Upvotes: 2

AnT stands with Russia
AnT stands with Russia

Reputation: 320491

The declaration says it returns a bool. The definition says it returns an ULong for some reason. That's your error, as the compiler told you already. Why did you switch the return type in the definition?

Upvotes: 3

Michael M.
Michael M.

Reputation: 2584

Your declaration returns a bool but your implementation returns a ULong !

Upvotes: 2

Related Questions