作者longrider (龙骑士2)
看板Programming
标题[问题] 请教 C++ 的基本问题
时间Wed Jan 6 18:06:08 2010
各位好
最近在学习 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
1F:→ adrianshum:public vs Public 202.155.236.82 01/06 18:08
2F:推 einspon:我用VC2008不只这错误,一堆错= = 123.195.32.199 01/06 22:19
3F:→ longrider:其实前两个错误就让我疑惑了 118.161.187.93 01/07 00:18