2012-08-11 62 views
0

我要編寫一個測試程序,測試上一個問題中設計的類上的各種操作;顯示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); 
} 

在這個問題上的任何幫助,將不勝感激。我在編程方面並不擅長,但我只是無法弄清楚爲什麼我會遇到這種類型的錯誤。迄今爲止,我還沒有上過我寫過的其他課程。

+0

你如何建立你的程序的一個例子嗎?這聽起來像你缺少一個'main()'函數。 – 2012-08-11 23:26:38

+0

沒有像DevC++這樣的C++編譯器。 – 2012-08-11 23:32:39

+0

你的意思是Visual Studio而不是DevC++? – joce 2012-08-11 23:36:50

回答

0

您缺少int main()功能。一個類沒有真正編譯成一個程序本身,你可以將它編譯成一個DLL,但不能編譯爲沒有main函數的可執行文件。更具體地說,該程序將編譯,但鏈接器抱怨它需要有一個入口點的程序。

如果你把它放在程序的底部:但這隻會導致未定義的引用消失。

int main(){ 
    return 0; 
} 

爲了實際測試你的程序,你需要運行一些測試,也許是這樣的:

#include <iostream> 

// your class code here. 

int main(){ 
    Clocktype clock; 
    clock.setTime(15, 42, 13); 
    int h, m, s; 
    clock.getTime(&h, &m, &s); 
    std::cout << h << m << s << std::endl; 
    return 0; 
} 
+0

您的建議解決方案是正確的,因爲它會使錯誤消失,但這隻會給OP帶來更多問題,因爲他的程序仍然不會執行任何操作。 – 2012-08-11 23:32:51

+0

這是真的,讓我更新它。 – Annabelle 2012-08-11 23:42:09

+0

好吧,現在我明白我錯過了什麼,我實際上錯過了整個可執行程序!在哪裏實際上將分配的變量並通過測試運行它們!謝謝,我知道我不應該得到這樣的事情,但是我沒有足夠的理解它的原因。謝謝。 – user1592770 2012-08-12 00:10:34

0

它不是一個編譯器,但一個鏈接錯誤,它說,連接器正試圖找到主要功能(爲Windows應用程序提供入口點)。

編譯本身確實工作正常(編譯成功後鏈接步驟工作)。

+0

好吧,我得到你說的這是一個鏈接錯誤,當我在查看「int main() {return 0;}部分,但是,這是我書中的一個例子,它不使用書中的int main ...在上面的例子中,我會應用那個部分,我在不同的地方嘗試過它並得到 – user1592770 2012-08-12 00:05:42

+0

您編寫一個類並編譯它,它會被轉換爲機器代碼,該機器代碼可以作爲庫的一部分(靜態.lib或動態.dll/.so)或作爲一個程序結束。一個程序的情況下,它需要由main定義的入口點。要從機器代碼中獲得一個庫或程序,必須將所需的參數傳遞給鏈接器(我不記得哪個是要指定的,它有已經很長時間了,因爲我已經使用C++,而且大多數IDE都會簡化它們) – SJuan76 2012-08-12 00:42:06

0

絕對有一個名爲Dev-C++的IDE,由Bloodshed公司製作,我相信它已經停產了,但仍然相當兼容並且很受歡迎,因爲它可以免費且方便地使用一個漂亮的界面。

至於OP的問題,您需要確定您的項目設置中有一個應用程序入口點(我多年未使用Dev-C++,因此請仔細查看),您會發現某些內容說明了應用程序類型,列出的選項會像

Console Application 
GUI Application or maybe Win32 Application 
Static Library 
Dynamic Library 

這些都是不同類型的應用程序,它在默認情況下看起來像你的程序Win32應用程序的設置,在這種情況下,鏈接正在尋找的WinMain但是你有沒有在您的項目中提供了一個,如果您希望簡單的控制檯應用程序將應用程序類型更改爲該設置,這將需要一個簡單的主函數,兩個應用程序入口點函數的示例Win32和控制檯是

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iShow) 
{ 
return 0; 
} 

或者一個簡單的控制檯應用程序

int main(int argc, char* argv[]) 
{ 
return 0; 
} 

這些功能,你需要在你的應用程序的其餘堵塞,從而爲它做任何事情,如果沒有這個切入點您的應用程序甚至無法正確編譯和鏈接。

使您的應用程序實際上使用你的類(不使用任何功能)

int main(int argc, char* argv[]) 
{ 
    clockType myClockType; 
    myClockType.setTime(12, 30, 45); 

    return 0; 
} 
+1

Dev-C + +是青睞的對立面。這是一個可怕的憎惡,任何人都不應該被卡住。 – Puppy 2012-08-12 14:47:32

+0

這是一個意見,Dev-C++可能已經過時,但它是一個非常完善的IDE,帶有一個很棒的插件包系統,很好的自動完成功能,並且在一點上具有良好的兼容性。只是說這是一個沒有理由的憎惡不是一個好的答案。 – user1294021 2012-08-16 02:41:40

+0

他們在Windows上創建了一個可替代的IDE,做得很好,我很想看看你已經構建的IDE以及它的優點。 Dev-C++也是在Windows上最容易與GTK一起使用的,但它永遠不會被構建,我想它會完全超出可用範圍。 – user1294021 2012-08-16 02:43:49