[問題] 請教 C++ 的基本問題
各位好
最近在學習 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
01/06 18:08, 1F
推
01/06 22:19, , 2F
01/06 22:19, 2F
→
01/07 00:18, , 3F
01/07 00:18, 3F
討論串 (同標題文章)
完整討論串 (本文為第 1 之 3 篇):