我对 C++ 编程比较陌生。我正在研究课程是如何工作的,但我对以下代码有疑问:
#include <iostream>
using namespace std;
class time
{
public:
time();
void settime (int, int, int);
void printuniversal ();
void printstandard ();
private:
int hour;
int minute;
int second;
};
time::time()
{
hour = minute = second = 0;
}
void time::settime (int h, int m, int s)
{
hour = (h >= 0 && h < 24) ? h : 0;
minute = (m >= 0 && m < 60) ? m : 0;
second = (s >= 0 && s < 60) ? 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;
cout << "Initial universal time: " << t.printuniversal();
cout << "\nInitial standard time: " << t.printstandard();
t.settime(13,27,6);
cout << "\nNew universal time: " << t.printuniversal();
cout << "\nNew standard time: " << t.printstandard();
return 0;
}
我得到的错误是:classi.cpp:42:6: error: expected ';' 在“t”之前 classi.cpp:43:39:错误:未在此范围内声明“t”
有什么我对课程不太了解的地方吗?为什么它不能识别 taa“时间”变量?