[問題] 主程式一呼叫標頭檔就出錯

看板C_and_CPP (C/C++)作者 (超級研究生)時間16年前 (2010/03/23 19:06), 編輯推噓5(507)
留言12則, 5人參與, 最新討論串1/1
遇到的問題: (題意請描述清楚) 我寫了一個主程式和一個標頭檔tenfive.h,若主程式沒呼叫標頭檔就能順利編譯,但 主程式只要加了#include "tenfive.h"就出現錯誤了。 希望得到的正確結果: 目前只要求能順利編譯… 程式跑出來的錯誤結果: 錯誤訊息如下: 11 C:\Dev-Cpp\work\hh6.cpp new types may not be defined in a return type 11 C:\Dev-Cpp\work\hh6.cpp extraneous `int' ignored 11 C:\Dev-Cpp\work\hh6.cpp `main' must return `int' 開發平台: (例: VC++ or gcc/g++ or Dev-C++, Windows or Linux) Dev C++ 有問題的code: (請善用置底文標色功能) 主程式 #include <iostream> using std::cin; using std::cout; using std::endl; #include "tenfive.h" int main() { enum Continue { Y, N }; Continue gameConti; gameConti = Y; while ( gameConti == Y ) { cout << "Try again?"; cout << "Enter \"y\" to try again, enter \"n\" to exit." << endl; int Conti; cin >> Conti; switch ( gameConti ) { case 'Y': case 'y': gameConti = Y; cout << "Play again!" << endl; case 'N': case 'n': cout << "Exit." << endl; gameConti = N; default: gameConti = Y; cout << "Play again!" << endl; } } return 0; } 標頭檔 //tenfive.cpp #include <iostream> using std::cin; using std::cout; using std::endl; #include <string> using std::string; #include <cstdlib> using std::rand; using std::srand; class tenfive { public: void DealCard1() { point1 = 8 + rand()%3; suit1 = rand()%4; } void DealCard2() { card2 = 1 + rand()%54; NoofCard += 1; point2 = 1 + card2%13; suit2 = card2 / 13; switch ( suit2 ) { case 0: cout << "You get A" << point2 << "." << endl; case 1: cout << "You get v" << point2 << "." << endl; case 2: cout << "You get []" << point2 << "." << endl; case 3: cout << "You get <^>" << point2 << "." << endl; case 4: cout << "You get joker." << endl; } } float 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 << "Now your point is " << allpoint << endl; } void DealOrEnd() { if ( allpoint >= 10.5 ) WinOrLose(); else if ( NoofCard >= 5 ) cout << "You win! (You get 5 cards.)" << endl; else DealAgain(); } void WinOrLose() { cout << " The banker's point is " << point1; if ( allpoint > 10.5 || allpoint <= point1 ) cout << "You lose!" << endl; else if ( allpoint == 10.5 || allpoint > point1 ) cout << "You win!" << endl; } void DealAgain() { int deal = 0; cout << "Enter \"1\" to deal again, otherwise start the gamble." << endl; cin >> deal; if ( deal == 1 ) { DealCard2(); pointSum(); } else WinOrLose(); } void settozero() { NoofCard = 0; allpoint = 0; } private: int card2; int NoofCard; int point1; float point2; float allpoint; int suit1; int suit2; } 補充說明: 遇到 `main' must return `int' 這個錯誤訊息該如何除錯,有請版友指導,謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.115.206.28

03/23 19:10, , 1F
最後那個問題, main的定義確保有回傳值int如
03/23 19:10, 1F

03/23 19:10, , 2F
int main(){...}, 並在main func的最後寫上reutrn 0
03/23 19:10, 2F

03/23 19:10, , 3F
記得這是C++開始對main的規定@_@"
03/23 19:10, 3F

03/23 19:12, , 4F
可是你的main看起來有了啊?? 詭異@_@"
03/23 19:12, 4F

03/23 19:12, , 5F
1. class 最後忘了加";" 2. pointSum()沒有return
03/23 19:12, 5F

03/23 19:13, , 6F
對啊 我把include "tenfive.h"拿掉就沒事了= =
03/23 19:13, 6F

03/23 19:14, , 7F
d大真是好眼力....orz
03/23 19:14, 7F

03/23 19:14, , 8F
所以型別不是void的最後都要加return喔?
03/23 19:14, 8F

03/23 19:15, , 9F
不然你覺得寫 float 的用意是什麼呢^^?
03/23 19:15, 9F

03/23 19:17, , 10F
謝謝兩位:) 話說d大眼力真的超強...
03/23 19:17, 10F

03/23 19:38, , 11F
其實第一條錯誤訊息大多數都是 class 忘了加 ; 才會出現的
03/23 19:38, 11F
※ 編輯: graduateme 來自: 140.115.206.28 (03/23 20:50)

03/23 23:11, , 12F
提醒一下, .h裡用using不是好習慣
03/23 23:11, 12F
文章代碼(AID): #1BgA2fyb (C_and_CPP)
文章代碼(AID): #1BgA2fyb (C_and_CPP)