[問題] 把標頭檔拆成函式原型和定義
遇到的問題: (題意請描述清楚)
各位大哥不好意思,小弟又有問題想請教
在昨天把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
03/24 16:34, 1F
→
03/24 16:34, , 2F
03/24 16:34, 2F
→
03/24 16:35, , 3F
03/24 16:35, 3F
→
03/24 16:35, , 4F
03/24 16:35, 4F
→
03/24 16:36, , 5F
03/24 16:36, 5F
→
03/24 16:36, , 6F
03/24 16:36, 6F
→
03/24 16:36, , 7F
03/24 16:36, 7F
→
03/24 16:37, , 8F
03/24 16:37, 8F
→
03/24 16:37, , 9F
03/24 16:37, 9F
→
03/24 16:38, , 10F
03/24 16:38, 10F
→
03/24 16:38, , 11F
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
03/24 16:44, 13F
→
03/24 16:44, , 14F
03/24 16:44, 14F
推
03/24 16:47, , 15F
03/24 16:47, 15F
→
03/24 16:47, , 16F
03/24 16:47, 16F
→
03/24 16:47, , 17F
03/24 16:47, 17F
→
03/24 16:48, , 18F
03/24 16:48, 18F
→
03/24 16:49, , 19F
03/24 16:49, 19F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章