[問題] 把標頭檔拆成函式原型和定義

看板C_and_CPP (C/C++)作者 (超級研究生)時間16年前 (2010/03/24 16:24), 編輯推噓3(3016)
留言19則, 3人參與, 最新討論串1/1
遇到的問題: (題意請描述清楚) 各位大哥不好意思,小弟又有問題想請教 在昨天把C++無法標頭檔的問題解決後,小弟我又嘗試把標頭檔分成函式原型、函式定義 .cpp檔的每個成員函式加上" (類別):: " 之後,卻編譯失敗 試了3、4個鐘頭,仍然不得其解,愈試愈灰心= = 煩請各位大大指導,謝謝! 希望得到的正確結果: 也是只求能順利編譯 程式跑出來的錯誤結果: 錯誤訊息如下: [Linker error] undefined reference to `tenfive::tenfive()' [Linker error] undefined reference to `tenfive::settozero()' [Linker error] undefined reference to `tenfive::DealCard1()' [Linker error] undefined reference to `tenfive::DealCard2()' [Linker error] undefined reference to `tenfive::pointSum()' [Linker error] undefined reference to `tenfive::DealOrEnd()' ld returned 1 exit status 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) Dev C++ 有問題的code: (請善用置底文標色功能) 函式原型: //tenfive.h class tenfive { public: void DealCard1(); void DealCard2(); void pointSum(); void DealOrEnd(); void WinOrLose(); void DealAgain(); void settozero(); private: int card2; int NoofCard; int point1; float point2; float allpoint; int suit1; int suit2; }; 函式定義: //tenfive.cpp #include <iostream> using std::cin; using std::cout; using std::endl; #include <cstdlib> using std::rand; using std::srand; #include "tenfive.h" void tenfive::DealCard1() { srand( time ( 0 )); suit1 = 0; point1 = 8 + rand()%3; suit1 = rand()%4; } void tenfive::DealCard2() { srand( time ( 0 )); point2 = 0; suit2 = 0; card2 = 1 + rand()%54; NoofCard += 1; point2 = 1 + card2%13; suit2 = card2 / 13; switch ( suit2 ) { case 4: cout << "You get joker." << endl; break; case 3: cout << "You get <^>" << point2 << "." << endl; break; case 2: cout << "You get []" << point2 << "." << endl; break; case 1: cout << "You get v" << point2 << "." << endl; break; case 0: cout << "You get A" << point2 << "." << endl; break; } } void tenfive::pointSum() { if ( point2 <= 13 && point2 >= 11 ) allpoint += 0.5; else if ( suit2 == 4 ) { if ( (allpoint + 10) > 10.5 ) allpoint += 0.5; else allpoint += 10; } else allpoint += point2; cout << "Your total point:" << setw(4) << allpoint << endl; } void tenfive::DealOrEnd() { if ( allpoint >= 10.5 ) WinOrLose(); else if ( NoofCard >= 5 ) cout << "You win! (You get 5 cards.)" << endl; else DealAgain(); } void tenfive::WinOrLose() { cout << "Banker's point: " << setw(4) << point1 << endl; if ( allpoint > 10.5 || allpoint <= point1 ) cout << "You lose!" << endl; else if ( allpoint == 10.5 ) cout << "You win! 10.5!" << endl; else cout << "You win!" << endl; } void tenfive::DealAgain() { char deal; cout << "Enter \"y\" to deal again, enter \"n\" to start the gamble. "; cin >> deal; cout << "\n"; switch ( deal ) { case 'Y': case 'y': DealCard2(); pointSum(); DealOrEnd(); break; default: WinOrLose(); } } void tenfive::settozero() { NoofCard = 0; allpoint = 0; } 補充說明: 順便問一樣大大們寫程式的習慣, 應該先寫函式原型、函式定義還是主程式, 或者是同時進行? 另外,有沒有什麼專門教debug的網站,不然我每次看到錯誤訊息, 也不知道如何下手才好…〒△〒 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.115.206.28

03/24 16:34, , 1F
所以你還有個類似main.cpp的會放main?? 你用DevCpp是不
03/24 16:34, 1F

03/24 16:34, , 2F
是直接開source file就編了沒有使用project功能??
03/24 16:34, 2F

03/24 16:35, , 3F
看仔細你的error是linker發的, linker在說它找不到error
03/24 16:35, 3F

03/24 16:35, , 4F
列的那些method的實體; 而這問題容易發生的理由是, 既然
03/24 16:35, 4F

03/24 16:36, , 5F
對啊…該不會需要什麼特殊操作吧= =||
03/24 16:36, 5F

03/24 16:36, , 6F
你有多個.cpp/.c, 就要讓每個.cpp/.c都compile過後並
03/24 16:36, 6F

03/24 16:36, , 7F
link起來才能產生可以用的執行檔/binary.
03/24 16:36, 7F

03/24 16:37, , 8F
通常開了project, IDE會自動compile所有.cpp/.c並幫你通
03/24 16:37, 8F

03/24 16:37, , 9F
通link起來, 不然就要自己懂/編寫makefile, 下參數
03/24 16:37, 9F

03/24 16:38, , 10F
compile個別的source file後, 再全部link以build出最後
03/24 16:38, 10F

03/24 16:38, , 11F
的執行檔/binary....@_@"
03/24 16:38, 11F
感謝 成功了 原來要丟進專案啊 我之前還在想說 函式原型都沒有主動呼叫函式定義,主程式居然能執行 讓我迷惑一陣子 ※ 編輯: graduateme 來自: 140.115.206.28 (03/24 16:46)

03/24 16:43, , 12F
酷~我剛好跟你有一樣的問題 照樓上講的把三個程式放進
03/24 16:43, 12F

03/24 16:44, , 13F
漏講了, 當然你要把相關的file加入那個project裡:)
03/24 16:44, 13F

03/24 16:44, , 14F
一個project 就可以執行了 XDDD
03/24 16:44, 14F

03/24 16:47, , 15F
理論上不用專案也可以, 就是我說的寫makefile自己處理
03/24 16:47, 15F

03/24 16:47, , 16F
V大一席話,救了一船人。
03/24 16:47, 16F

03/24 16:47, , 17F
compile的與link的動作; 不過沒有特殊需求的話, 就讓IDE
03/24 16:47, 17F

03/24 16:48, , 18F
自己處理方便很多就是, 以後也許有機會再學makefile:)
03/24 16:48, 18F

03/24 16:49, , 19F
(搖搖手) 小弟我只是不務正業路過的.... XD
03/24 16:49, 19F
文章代碼(AID): #1BgSmrSs (C_and_CPP)
文章代碼(AID): #1BgSmrSs (C_and_CPP)