0

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.

4

4 回答 4

1

在您的代码中,

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

上面两个都可以不带任何参数调用,所以都是默认构造函数

由于您使用的是默认构造,因此编译器无法知道您的意思是上述哪一项。

这是模棱两可的。

一种解决方案是删除参数默认值。

顺便说一句,你operator++有一个小问题;彻底测试发现问题!;-)

于 2012-08-12T03:26:13.467 回答
1

如果没有参数,您的两个构造函数都是候选者。

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.

这也不是你所期望的:

clockType yourClock();

这是一个接受零参数并返回一个时钟类型对象的函数的前向声明。你真正的意思是:

clockType yourClock;

// or
clockType yourClock = clockType();

这被称为“最令人烦恼的解析”问题。

于 2012-08-12T02:49:22.920 回答
0
clockType ();
clockType (int hours = 0, int minutes = 0, int seconds = 0);

这些都可以用 0 个参数构造。

很有可能,您应该摆脱 ,clockType();因为我假设这两个构造函数在不带参数的情况下无论如何都会做同样的事情。

于 2012-08-12T02:46:07.950 回答
0

此错误似乎与运算符声明无关。定义的构造函数为您创造了一个模棱两可的情况。

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

调用 clockType() 时的意图是什么?根据上面的定义,有两个签名可以匹配您的请求。在没有构造函数参数的情况下,无参数版本和具有所有默认值的版本在逻辑上都可以工作。您需要确定他们的个人意图是什么,然后编辑他们的定义以匹配。

于 2012-08-12T02:46:28.977 回答