我知道某处有这样一个问题的线程,但它并没有真正帮助我这就是为什么。我有我的课:和我的主要课程,我不知道为什么它仍然给我这个错误
班级 :
class MyTime {
public:
MyTime(int =0 , int = 0 , int = 0); // constructor
void setTime(int , int , int); //set hour , minute,seconds
void setHours(int); //set hour after validation
void setMinutes(int); //set minute after validation
void setSeconds(int); //set seconds after validation
//get function
int getHours();//return hours
int getMinutes();//return minutes
int getSeconds(); //return seconds
void printUniversal(); //output time in universal time format
void printStandard(); //output time in standart time format
private:
int hours; // 0-23(24 hour clock format)
int minutes; // 0 -59
int seconds; // 0-59
};
主要的
#include <iostream>
#include "time.h"
using namespace std;
int main()
{
MyTime t1;
MyTime t2(2);
MyTime t3(21,34);
MyTime t4(12,25,42);
MyTime t5(27,67,78);
cout<< "Constructed with:\n\nt1 : default \n";
t1.printUniversal();
cout<<"\n";
t1.printStandard();
cout<<"t2 \n";
t2.printUniversal();
cout<<"\n";
t2.printStandard();
cout <<"\nt3\n";
t3.printUniversal();
cout<<"\n";
t3.printStandard();
cout<<"\nt4\n";
t4.printUniversal();
cout<<"\n";
t4.printStandard();
cout<<"\nt5\n";
t5.printUniversal();
cout<<"\n";
t5.printStandard();
cout<<endl;
}
函数文件:
#include <iostream>
#include <iomanip>
#include "time.h"
using namespace std;
//Time constructor initializes each data member to zero
//ensure that Time objects start in a consistent state
MyTime::MyTime(int hr,int min,int sec){
setTime(hr,min,sec); //validate and set time
}
//set new time value using universal time, ensure that
//the data remains consistent by setting invalid values to zero
void MyTime::setTime(int h , int m , int s){
setHours(h);//set private field hours
setMinutes(m); //set private field minutes
setSeconds(s); //set private field seconds
}
void MyTime::setHours(int h){
hours = (h >= 0 && h < 24) ? h : 0 ; //validate hours
}
void MyTime::setMinutes(int m){
minutes = (m>=0 && m < 60 ) ? m : 0; //validate minutes
}
void MyTime::setSeconds(int s){
seconds=(s >= 0 && s < 60)? s : 0; //validate seconds
}
int MyTime::getHours(){
return hours;
}
int MyTime::getMinutes(){
return minutes;
}
int MyTime::getSeconds(){
return seconds;
}
// print time in universal time (HH:MM:SS)
void MyTime::printUniversal(){
cout << setfill('0') <<setw(2) <<getHours() <<":"<< setw(2)<<getMinutes() <<":" <<setw(2) <<getSeconds();
}
//print ime in Standard time (HH:MM:SS PM/AM)
void MyTime::printStandard(){
cout<< ((getHours() == 0 || getHours() ==12) ? 12 :getHours() % 12)
<<":" <<setfill('0')<<setw(2)<<getMinutes()
<<":" <<setw(2)<<getSeconds()<<(hours <12 ? "AM" : "PM");
}
错误将是预期的;在 t1,t2,t3,t4 之前。
任何帮助将不胜感激 。