2012-08-12 131 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