好的,这是我编辑的代码,但现在每次编译时都会出现分段错误(核心转储)。我哪里出错了?我不确定我是否正确地“回溯”了它,但这就是我尝试得到的结果:程序收到信号 SIGSEGV,分段错误。0x000109f0 in MyTime::MyTime (this=, h=, m=) at MyTime.cc:10 10 MyTime::MyTime(int h, int m){
//MyTime.h File
#include <iostream>
class MyTime
{
public:
MyTime(int h = 0, int m = 0);
void Reset(int h, int m);
void input();
void output() const;
MyTime operator + (const MyTime& t1) const;
MyTime operator - (const MyTime& t1) const;
MyTime operator * (const int& num) const;
MyTime operator / (const int& num) const;
bool operator == (const MyTime& t1) const;
bool operator < (const MyTime& t1) const;
bool operator <= (const MyTime& t1) const;
int get_hours() const{return hours;}
int get_minutes() const{return minutes;}
private:
void simplify();
int hours; // hours can be > 24
int minutes; // 0 <= minutes <= 59
};
std::istream& operator >>(std::istream& fin, MyTime& t);
std::ostream& operator <<(std::ostream& fout, const MyTime& t);
//MyTime.cc File
#include "MyTime.h"
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
// Constructor
MyTime::MyTime(int h, int m){
hours = h;
minutes = m;
}
void MyTime::Reset(int h, int m){
hours = h;
minutes = m;
}
void MyTime::simplify(){
hours += minutes/60;
minutes = minutes%60;
}
void MyTime::input(){
char junk;
cin >> hours;
cin.get(junk);
cin >> minutes;
simplify();
}
void MyTime::output() const{
cout << hours << ':' << setw(2) << setfill('0') << minutes;
}
MyTime MyTime::operator +(const MyTime& t1) const{
MyTime tmp;
tmp.hours = t1.hours + hours;
tmp.minutes = t1.minutes + minutes;
tmp.simplify();
return tmp;
}
MyTime MyTime::operator -(const MyTime& t1) const{
MyTime tmp;
tmp.minutes = abs((t1.hours*60+t1.minutes) -
(hours*60+minutes));
tmp.simplify();
return tmp;
}
MyTime MyTime::operator /(const int& num) const{
MyTime tmp;
tmp.minutes = hours * 60 + minutes;
tmp.minutes /= num;
tmp.simplify();
return tmp;
}
MyTime MyTime::operator *(const int& num) const{
MyTime tmp;
tmp.minutes = hours * 60 + minutes;
tmp.minutes *= num;
tmp.simplify();
return tmp;
}
bool MyTime::operator == (const MyTime& t1) const{
return t1.hours == hours && t1.minutes == minutes;
}
bool MyTime::operator < (const MyTime& t1) const{
return (t1.hours * 60 + t1.minutes) < (hours * 60 + minutes);
}
bool MyTime::operator <=(const MyTime& t1) const{
return (t1 == (hours * 60 + minutes)) || (t1 < (hours * 60 + minutes));
}
ostream& operator <<(ostream& fout, const MyTime& t){
t.output();
return fout;
}
istream& operator >>(istream& fin, MyTime& t){
t.input();
return fin;
}
//main.cc File
#include <iostream>
#include "MyTime.h"
int main()
{
MyTime t1, t2;
int scalar;
std::cout << "Enter a time: ";
std::cin >> t1;
std::cout << "Enter another time: ";
std::cin >> t2;
std::cout << "Enter a scalar to manipulate those times: ";
std::cin >> scalar;
if(t1 == t2)
std::cout << t1 << " is equal to " << t2 << std::endl;
if(t1 < t2)
std::cout << t1 << " is less than " << t2 << std::endl;
if(t1 <= t2)
std::cout << t1 << " is less than or equal to " << t2 << std::endl;
std::cout << t1 << " + " << scalar << " = " << t1 + scalar << std::endl;
std::cout << t1 << " - " << scalar << " = " << t1 - scalar << std::endl;
std::cout << t1 << " * " << scalar << " = " << t1 * scalar << std::endl;
std::cout << t1 << " / " << scalar << " = " << t1 / scalar << std::endl;
std::cout << t2 << " + " << scalar << " = " << t2 + scalar << std::endl;
std::cout << t2 << " - " << scalar << " = " << t2 - scalar << std::endl;
std::cout << t2 << " * " << scalar << " = " << t2 * scalar << std::endl;
std::cout << t2 << " / " << scalar << " = " << t2 / scalar << std::endl;
return 0;
}