我知道这可能以前被问过,但不幸的是我无法调试错误。
我写了一个时间类:
class time
{
public:
time(); //constructor
void setTime (int, int, int); //set time
void dispTime(); //print time
private:
int hour, minute, second;
};
然后我实现函数成员:
#include <iostream>
#include "stdio.h"
#include "time.h"
time :: time()
{
hour = 12;
minute = 0;
second = 0;
}
//**********
void time::setTime(int h, int m, int s)
{
hour = (h >= 0 && h < 12) ? h : 0;
minute = (m >= 0 && m < 60) ? m : 0;
second = (s >= 0 && s < 60) ? s : 0;
}
//**********
void time::dispTime()
{
std::cout << ((hour == 0 || hour == 12) ? 12 : hour % 12)
<< " : " << (minute < 10 ? "0" : "") << minute
<< " : " << (second < 10 ? "0" : "") << second
<< (hour < 12 ? " AM" : " PM");
}
最后是主体主体:
#include <iostream>
#include "stdio.h"
#include "time.h"
using namespace std;
//**********
int main()
{
time T;
cout << "The initial standard time is: ";
T.dispTime();
T.setTime(13, 27, 36);
cout << "\nStandard time after set-time is: ";
T.dispTime();
T.setTime(99,83,12); //attemp to have a invalid time
cout << "\nStandard time is invalid and standard time is: ";
T.dispTime();
cin.get();
cin.get();
}
当我用 g++ 编译它时:
4-5-class-time.cpp:在函数'int main()'中:
4-5-class-time.cpp:8: 错误: 预期 `;' 在'T'之前</p>
4-5-class-time.cpp:10:错误:未在此范围内声明“T”
在此先感谢您的帮助!