[問題] 請教 C++ 的基本問題

看板Programming作者 (龍騎士2)時間14年前 (2010/01/06 18:06), 編輯推噓1(102)
留言3則, 3人參與, 最新討論串1/3 (看更多)
各位好 最近在學習 c++ 的 class 以下是課本的範例, 課本是說用在 Visual C++ 6.0, 但我用 gcc compile --------------------------------------------------------- #include <iostream> using std::cout; using std::endl; class Time { //第六行,forward declaration of 'class Time' Public: Time(); //第八行,invalid use of incomplete type 'class Time' void setTime(int, int, int); void printUniversal(); void printStandard(); Private: int hour; int minute; int second; }; Time::Time(){ hour = minute = second = 0; } Time::setTime(int h, int m, int s) { hour = ((h>=0)&&(h<=23)?h:0); min = ((m>=0)&&(m<=59)?m:0); sec = ((s>=0)&&(s<=59)?s:0); } void Time::printUniversal() { cout << hour << ":" << minute << ":" << second << endl; }; void Time::printStandard() { cout << ((hour == 0 || hour == 12) ? 12 : hour % 12) << ":" << minute << ":" << second << (hour < 12 ? "AM": "PM") << endl; }; int main () { Time t; t.printUniversal(); t.printStandard(); t.settime(23,59,59); t.printUniversal(); t.printStandard(); } -------------------------------------------------------------- 但是用 gcc compile 後, 卻出現有錯誤訊息 -------------------------------------------------------------- c.cpp:8: error: invalid use of incomplete type ‘class Time’ c.cpp:6: error: forward declaration of ‘class Time’ c.cpp:8: error: ISO C++ forbids declaration of ‘Public’ with no type c.cpp:14: error: expected primary-expression before ‘int’ ...略 -------------------------------------------------------------- 根據課本的說法, 在 class 裡面宣告一個跟 class 一樣名稱的函數 該函數可將會作為初始化這個 class 之用 但錯誤訊息卻不是這樣表示.... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.115.34.222

01/06 18:08, , 1F
public vs Public
01/06 18:08, 1F

01/06 22:19, , 2F
我用VC2008不只這錯誤,一堆錯= =
01/06 22:19, 2F

01/07 00:18, , 3F
其實前兩個錯誤就讓我疑惑了
01/07 00:18, 3F
文章代碼(AID): #1BH62ILr (Programming)
文章代碼(AID): #1BH62ILr (Programming)