2012-08-12 84 views
0

我终于得到我的任务几乎完成,但是现在当我编译它时,我得到一组全新的错误。C++模糊运算符过载错误

#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; 
} 

,我得到的错误是:

在函数main()的;
重载'clockType()'的调用是不明确的候选者是:clockType :: clockTYpe(int,int,int)clockType :: clockType()。

我不确定这是什么问我或错误是什么。

回答

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

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

机会是,你应该摆脱clockType();,因为我假设这两个构造函数无论如何都会在没有参数的情况下调用相同的东西。

0

此错误似乎与操作员声明没有任何关系。定义的构造函数为您创建了一个模糊的情况。

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

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

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的对象。你真正的意思是:

clockType yourClock; 

// or 
clockType yourClock = clockType(); 

这被称为"Most Vexing Parse"问题。

+0

即使我使用clockType yourClock;或clockType yourClock = clockTYpe();第一个是他们在我的文本中给出的例子,我得到了同样的错误。 – user1592770 2012-08-12 03:14:30

+0

@ user1592770:您需要解决这两个问题。 1)从你的类中移除构造函数'clockType();'这样就没有意义了。 2)将'yourClock'的定义更改为上述之一。 – 2012-08-12 06:13:01

+0

谢谢;它解决了这个问题,它现在编译并运行,并为我提供了我正在寻找的输出。再次感谢。 – user1592770 2012-08-12 11:12:21

1

在你的代码,

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

都可以称为不带任何参数,所以两者都默认构造

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

这是不明确的。

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

顺便说一下,您的operator++有一个小问题;彻底测试以发现问题!;-)

+0

感谢您的帮助...顺便说一句,我没有找到任何东西在运营商++ – user1592770 2012-08-12 11:12:56

+0

哦,它返回的结果*值*。更好地返回一个引用(像内建类型的'++')或者有'void'结果(用最少的代码来获取并且不支持带有多重副作用的表达式)。 – 2012-08-12 13:59:43

+0

请参阅:http://stackoverflow.com/a/7436568/14065 – 2012-08-12 14:59:19