2015-04-02 114 views
0

我已閱讀:undefined reference to `[email protected]' &仍然不明白我的問題。未定義引用WinMain @ 16 - 代碼塊

我有一個工作的程序。添加了一個類,但沒有將它實現到程序中,而只是寫了頭文件和.cpp文件。剛剛添加這個類的程序工作,現在不行。

錯誤狀態...... **文件地址.... libmingw.32.a(main.o),此:main.c中:(text.startup + 0xa7)

頭文件

 #ifndef DATE_H 
     #define DATE_H 

     #include <iostream> 
     #include <string> 

     class Date 
     { 
      public: 
       Date(); 

       //Setters 
       void SetDay(unsigned dy); 

       void SetMonth(std::string month); 

       void SetYear(unsigned mnth); 

       //Getters 
       unsigned GetDay() const; 

       std::string GetMonth() const; 

       unsigned GetYear() const; 

      private: 
       unsigned day, year; 
       std::string month; 
     }; 

     #endif // DATE_H 

.cpp文件

 #include "Date.h" 

     Date::Date() 
     { 
      day = 0; 
      year = 0; 
      month = "Not Set"; 
     } 

     //Setters 
     void Date::SetDay(unsigned dy) 
     { 
      day = dy; 
     } 

     void Date::SetMonth(std::string mnth) 
     { 
      month = mnth; 
     } 

     void Date::SetYear(unsigned yr) 
     { 
      year = yr; 
     } 

     //Getters 
     unsigned Date::GetDay() const 
     { 
      return day; 
     } 

     unsigned Date::GetYear() const 
     { 
      return year; 
     } 

     std::string Date::GetMonth() const 
     { 
      return month; 
     } 

我的主,我在稱呼它,只是因爲我不知道是否是因爲它沒有被稱爲誤差或類似的東西是:

 #include <iostream> 
     #include <fstream> 
     #include "unit.h" // Unit class declaration 
     #include "regist.h" // Registration class declaration 
     #include "date.h" 

     using namespace std; 

     // Main program: 
     // Open an input file stream, read a Registration object, 
     // including its list of courses. Redisplay all information, 
     // and calculate the total credits taken. Write the results 
     // to a file stream. 


     int main() 
     { 
      ifstream infile("testin.txt"); 
      if(!infile) return -1; 

      Registration R; 
      Date D; 
      infile >> R; 

      ofstream ofile("testout.txt"); 

      ofile << R; 

      cout << "\nComputation completed. Check output file. \n"; 
      cout << "\n Day is " << D.GetDay; 

      return 0; 
     } 

不會在與註冊有關的超載>>操作符中設置日期。它由基本的構造函數設置。

我再次沒有將這個類加入到程序中,因爲它只是在編寫和添加基本測試方式後才編譯。通過主測試。

在此先感謝。 = D

+0

因此,它僅適用於主要功能,但不適用於添加的類。你確定你編譯並鏈接了兩個.cpp文件嗎? – chris 2015-04-02 04:30:43

+0

即使該類未鏈接,它只是項目的一部分,但未被使用。它仍然有相同的錯誤。 – 2015-04-02 06:54:39

+0

是否有可能已經有一個名爲date.h的庫,因此出現錯誤? – 2015-04-03 01:54:11

回答

1

問題是,您的新項目已創建爲Win32 GUI project,應該將其創建爲Console application。前者要求存在具有簽名int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)的函數,而後者需要C或C++項目採用的通常形式之一,即int main()int main(char *argv[], int argc)

要麼創建一個控制檯類型的新項目並將代碼複製到其中,或者使用'膠帶'解決方案,並將int main()更改爲int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)您還需要導航到項目的屬性並更改構建目標從GUI applicationConsole application - 如果沒有這樣做,則意味着您將看不到任何輸出,因爲您使用的是cout,它會打印到控制檯顯示的stdout。此窗口始終可用於控制檯應用程序,但僅適用於GUI應用程序的Debug版本。

我建議正確地做,並創建一個適當類型的新項目,即Console application。 :)

+0

無論如何檢查項目是作爲什麼?我正在使用別人給我的項目。 – 2015-04-02 06:55:07

+0

此外,它爲什麼會在以前和當我添加日期不工作。在「它已被創建爲Win32 GUI項目」的假設下? – 2015-04-02 06:59:04

+0

不確定,對不起。 :| – enhzflep 2015-04-02 09:53:00

0

將您的項目設置下的應用程序類型更改爲控制檯。 WinMain16與Windows圖形應用程序有關。我相信你需要設置一個特殊的預處理器標誌或者包含一個某種類型的庫,如果你把它保存爲一個Windows圖形應用程序,它可以正常工作,但在這種情況下,最簡單的解決方法是獲取一個控制檯應用程序。

此外,也許,添加-mwindows標誌可以幫助你。

+1

它是PE頭中的一個標誌,由'-Wl, - subsystem,windows'設置。 – a3f 2015-04-02 07:52:01

+0

OP包含的鏈接解釋了爲什麼這不是必需的。 – chris 2015-04-02 12:29:39

相關問題