[問題] 教科書例子class介面實作分開不能compile的問題

看板C_and_CPP作者 (sdimkk)時間15年前 (2010/09/08 15:12), 編輯推噓0(0010)
留言10則, 3人參與, 最新討論串1/1
用的是visual c++ 6.0, 最近在看class 介面實作分離的部分, 看到一個教科書的例子: 就是輸出設定的時間, 用軍用格式 08:00, 19:20 輸出和 通用格式 08:00 AM 07:20 PM輸出 這樣 內容很簡單,但是我直接用教科書光碟裡的程式執行竟沒辦法使用 因此想請問一下。 以下是程式碼,os是windows: // Fig. 6.5: time1.h // Declaration of the Time class. // Member functions are defined in time1.cpp // prevent multiple inclusions of header file #ifndef TIME1_H #define TIME1_H // Time abstract data type definition class Time { public: Time(); // constructor void setTime( int, int, int ); // set hour, minute, second void printMilitary(); // print military time format void printStandard(); // print standard time format private: int hour; // 0 - 23 int minute; // 0 - 59 int second; // 0 - 59 }; #endif _______________以上是class的宣告定義,存在time1.h的檔名下 // Fig. 6.5: time1.cpp // Member function definitions for Time class. #include <iostream> using std::cout; #include "time1.h" // Time constructor initializes each data member to zero. // Ensures all Time objects start in a consistent state. Time::Time() { hour = minute = second = 0; } // Set a new Time value using military time. Perform validity // checks on the data values. Set invalid values to zero. void Time::setTime( int h, int m, int s ) { hour = ( h >= 0 && h < 24 ) ? h : 0; minute = ( m >= 0 && m < 60 ) ? m : 0; second = ( s >= 0 && s < 60 ) ? s : 0; } // Print Time in military format void Time::printMilitary() { cout << ( hour < 10 ? "0" : "" ) << hour << ":" << ( minute < 10 ? "0" : "" ) << minute; } // Print time in standard format void Time::printStandard() { cout << ( ( hour == 0 || hour == 12 ) ? 12 : hour % 12 ) << ":" << ( minute < 10 ? "0" : "" ) << minute << ":" << ( second < 10 ? "0" : "" ) << second << ( hour < 12 ? " AM" : " PM" ); } _________以上是class的public函示成員,存在time1.cpp的檔名下 // Fig. 6.5: fig06_05.cpp // Driver for Time1 class // NOTE: Compile with time1.cpp #include <iostream> using std::cout; using std::endl; #include "time1.h" // Driver to test simple class Time int main() { Time t; // instantiate object t of class time cout << "The initial military time is "; t.printMilitary(); cout << "\nThe initial standard time is "; t.printStandard(); t.setTime( 13, 27, 6 ); cout << "\n\nMilitary time after setTime is "; t.printMilitary(); cout << "\nStandard time after setTime is "; t.printStandard(); t.setTime( 99, 99, 99 ); // attempt invalid settings cout << "\n\nAfter attempting invalid settings:\n" << "Military time: "; t.printMilitary(); cout << "\nStandard time: "; t.printStandard(); cout << endl; return 0; } ______________以上main()存在Fig06_05.cpp的檔名下_____________ 我把這三個檔案都放在c的同一個資料夾下後執行main() 結果error: Fig06_05.obj : error LNK2001: unresolved external symbol "public: void __ thiscall Time::setTime(int,int,int)" (?setTime@Time@@QAEXHHH@Z) Fig06_05.obj : error LNK2001: unresolved external symbol "public: void __ thiscall Time::printStandard(void)" (?printStandard@Time@@QAEXXZ) Fig06_05.obj : error LNK2001: unresolved external symbol "public: void __ thiscall Time::printMilitary(void)" (?printMilitary@Time@@QAEXXZ) Fig06_05.obj : error LNK2001: unresolved external symbol "public: __ thiscall Time::Time(void)" (??0Time@@QAE@XZ) Debug/Fig06_05.exe : fatal error LNK1120: 4 unresolved externals Error executing link.exe. Fig06_05.exe - 5 error(s), 0 warning(s) 三個都放c下同一個資料夾裡後執行main(),結果如上,而且還是教科書的,都沒改。 請問是沒有一起compile到嗎? 這邊沒解決就沒辦法繼續自讀下去,拜託各方高手幫忙一下,謝謝。 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 122.120.134.40

09/08 15:23, , 1F
在IDE裡建project 或g++後面接三個檔案
09/08 15:23, 1F

09/08 15:36, , 2F
開啟一個cpp檔後按編譯他就會問你要不要建預設專案
09/08 15:36, 2F

09/08 15:38, , 3F
接著在FileView底下點專案右鍵'Add Files to Project'
09/08 15:38, 3F

09/08 15:39, , 4F
該有的檔案都加進來後, 編譯完就會按連結就會把不同部
09/08 15:39, 4F

09/08 15:39, , 5F
份整合在一起了
09/08 15:39, 5F

09/08 15:40, , 6F
^Build
09/08 15:40, 6F

09/08 16:43, , 7F
感謝兩位 已經解決了~^^但似乎每次開cpp都要重複add file?
09/08 16:43, 7F

09/08 16:52, , 8F
不用啊 project存起來 下次可以直接開project檔
09/08 16:52, 8F

09/08 16:52, , 9F
如果是下command 可以用makefile
09/08 16:52, 9F

09/08 18:01, , 10F
哦哦 多謝啦
09/08 18:01, 10F
文章代碼(AID): #1CXpTvi3 (C_and_CPP)