简体   繁体   中英

C++ ambiguous operator overload error

I am finally getting my assignment to be almost complete, however now I am getting a whole new set of errors when I compile it.

#include <iostream>

using namespace std;

class clockType
{
      friend ostream& operator<<(ostream&, const clockType&);
      friend istream& operator>>(istream&, clockType&);
public:
      void setTime (int hours, int minutes, int seconds);
      void getTime (int& hours, int& minutes, int& seconds) const;     
      clockType operator++();
      bool operator==(const clockType& otherClock) const;
      bool operator!= (const clockType& otherClock) const;
      bool operator<=(const clockType& otherClock) const;
      bool operator<(const clockType& otherClock) const;
      bool operator>=(const clockType& otherClock) const;
      bool operator>(const clockType& otherClock) const;
      clockType ();
      clockType (int hours = 0, int minutes = 0, int seconds = 0);
private:
        int hr;
        int min;
        int sec;
};
clockType clockType::operator++()
{
          sec++;
          if (sec > 59)
          {
              sec = 0;
              min++;
              if (min > 59)
              {
                      min = 0;
                      hr++;
                      if (hr > 23)
                      hr = 0;
              }
          }
          return *this;
}
bool clockType::operator==(const clockType& otherClock) const
{
     return (hr == otherClock.hr && min == otherClock.min && sec == otherClock.sec);
}
bool clockType::operator<=(const clockType& otherClock) const
{
     return ((hr < otherClock.hr) || (hr == otherClock.hr && min < otherClock.min) || (hr == otherClock.hr && min == otherClock.min && sec <= otherClock.sec));
}
bool clockType::operator!=(const clockType& otherClock) const
{
          return (hr != otherClock.hr || min != otherClock.min || sec != otherClock.sec);
}
bool clockType::operator<(const clockType& otherClock) const
{
     return ((hr < otherClock.hr) || (hr == otherClock.hr && min < otherClock.min) || (hr == otherClock.hr && min == otherClock.min && sec < otherClock.sec));
}
bool clockType::operator>=(const clockType& otherClock) const
{
     return ((hr > otherClock.hr) || (hr == otherClock.hr && min > otherClock.min) || (hr == otherClock.hr && min == otherClock.min && sec >= otherClock.sec));
}
bool clockType::operator>(const clockType& otherClock) const
{
     return ((hr > otherClock.hr) || (hr == otherClock.hr && min > otherClock.min) || (hr == otherClock.hr && min == otherClock.min && sec > otherClock.sec));
}

void clockType::setTime(int hours, int minutes, int seconds)
{
     if (0 <= hours && hours < 24)
     hr = hours;
     else
     hr = 0;
     if (0 <= minutes && minutes < 60)
     min = minutes;
     else
     min = 0;
     if (0 <= seconds && seconds < 60)
     sec = seconds;
     else
     sec = 0;
}
void clockType::getTime(int& hours, int& minutes, int& seconds)const
{
     hours = hr;
     minutes = min;
     seconds = sec;
}
clockType::clockType(int hours, int minutes, int seconds)
{
 setTime(hours, minutes, seconds);
}
ostream& operator<<(ostream& osObject, const clockType& timeOut)
{
         if (timeOut.hr < 10)
         osObject << '0';
         osObject << timeOut.hr << ':';
         if (timeOut.min < 10)
         osObject << '0';
         osObject << timeOut.min << ':';
         if (timeOut.sec < 10)
         osObject << '0';
         osObject << timeOut.sec << ':';
         return osObject;
}
istream& operator>>(istream& is, clockType& timeIn)
{
         char ch;
         is >> timeIn.hr;
         if (timeIn.hr < 0 || timeIn.hr >=24)
         timeIn.hr = 0;
         is.get(ch);
         is >> timeIn.min;
         if (timeIn.min < 0 || timeIn.min >= 60)
         timeIn.min = 0;
         is.get(ch);
         is >> timeIn.sec;
         if (timeIn.sec < 0 || timeIn.sec >= 60)
         timeIn.sec = 0;
         return is;
}
int main() 
{ 
   clockType  myClock(4, 9, 22);
   clockType yourClock ();

   cout << "myClock = " << myClock << endl;
   cout << "yourClock = " << yourClock << endl;
   cout << "enter the time in form " << "hr:min:sec ";
   cin >> myClock;
   cout << endl;
   cout << "The new time of myClock = " << myClock << endl; 
   ++myClock;
   cout << "After incrementing the time, " << "myClock = " << myClock << endl;
   yourClock.setTime(15, 20, 25);
   cout << "After setting the time, " << "yourClock = " << yourClock << endl;
   if (myClock == yourClock)
   cout << "The times of myClock and " << "yourClock are equal." << yourClock << endl;
   else
   cout << "The times of myClock and " << "yourClock are not equal." << endl;
   if (myClock <= yourClock)
   cout << "The time of myClock is " << "less than or equal to " << endl << "the time of yourClock " << endl;
   else
   cout << "The time of myClock is " << "greater than the time of " << "yourClock." << endl;
   return 0;
}

The error that I am getting is:

In funct main();
call of overloaded 'clockType()' is ambiguous candidates are: clockType::clockTYpe(int, int, int) clockType::clockType().

I am unsure of what this is asking of me or what the error really is.

Both your constructors are candidates if there are no parameters.

clockType();                                                 // takes zero parameters.
clockType (int hours = 0, int minutes = 0, int seconds = 0); // can take zero parameters.

// Thus the compiler does not know whaich one to call.

Also this is not what you expect:

clockType yourClock();

This is a forward declaration of a function taking zero arguments and return an object of clockType. What you really mean is:

clockType yourClock;

// or
clockType yourClock = clockType();

This is referred to as the "Most Vexing Parse" problem.

In your code,

  clockType ();
  clockType (int hours = 0, int minutes = 0, int seconds = 0);

both of the above can be called without any argument, so both are default constructors .

Since you're using default construction, the compiler can't know which one of the above you meant.

It's ambiguous.

One solution is to remove the argument defaulting.

By the way, your operator++ has a little problem; test it thoroughly to find the problem! ;-)

clockType ();
clockType (int hours = 0, int minutes = 0, int seconds = 0);

These can both be constructed with 0 arguments.

Chances are, you should just get rid of the clockType(); as I'm assuming these two constructors would do the same thing anyway when called with no arguments.

This error doesn't appear to have anything to do with the operator declarations. The constructors that are defined have created an ambiguous situation for you.

clockType ();
clockType (int hours = 0, int minutes = 0, int seconds = 0);

What is the intent when you call clockType() ? With the definitions above there are two signatures that would match your request. With no constructor parameters both the parameterless version and the version with all defaults would logically work. You need to determine what their individual intent is and then edit their definitions to match.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM