我要編寫一個測試程序,測試上一個問題中設計的類上的各種操作;顯示clockType
重載作爲成員函數的定義。當我使用Dev C++編譯器進行編譯時,出現以下錯誤。DevC++編譯器錯誤
錯誤讀取:
[link error] undefined reference "[email protected]'
Id returned 1 exit status
這是我的代碼:
#include <iostream>
using namespace std;
class 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);
}
在這個問題上的任何幫助,將不勝感激。我在編程方面並不擅長,但我只是無法弄清楚爲什麼我會遇到這種類型的錯誤。迄今爲止,我還沒有上過我寫過的其他課程。
你如何建立你的程序的一個例子嗎?這聽起來像你缺少一個'main()'函數。 – 2012-08-11 23:26:38
沒有像DevC++這樣的C++編譯器。 – 2012-08-11 23:32:39
你的意思是Visual Studio而不是DevC++? – joce 2012-08-11 23:36:50