高手救命啊!!!->(重载运算符出错)
头文件:
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class newtime24
{
public:
newtime24(int h=0, int m =0):hour(0),minute(0)
{
normalizeTime();
}
//一组+运算符 ++++++++++++++
friend newtime24 operator+(const newtime24& lhs, const newtime24& rhs)
{
return newtime24(lhs.hour+rhs.hour,lhs.minute+rhs.minute);
}
friend newtime24 operator+(const newtime24& lhs,int min)
{
return newtime24(lhs.hour,lhs.minute+min);
}
friend newtime24 operator+(int min, const newtime24& rhs)
{
return newtime24(rhs.hour,min+rhs.minute);
}
// - - - -- -
friend newtime24 operator- (const newtime24& lhs, const newtime24& rhs)
{
int currTime = lhs.hour*60 +lhs.minute;
int tTime = rhs.hour*60 + rhs.minute;
if(tTime < currTime)
throw string("time24 duration():argument is an earlier time");
else
return newtime24(0,tTime-currTime);
}
friend bool operator==(const newtime24& lhs, const newtime24& rhs)
{
return (lhs.hour*60 +lhs.minute)== ( rhs.hour*60 + rhs.minute);
}
//
friend bool operator<(const newtime24& lhs, const newtime24& rhs)
{
return (lhs.hour*60 +lhs.minute)<( rhs.hour*60 + rhs.minute);
}
// iostream
friend ostream& operator<< (ostream& ostr, const newtime24& t)
{
ostr << setfill('0') << t.hour << ":" << setw(2) << t.minute;
return ostr;
}
friend istream& operator>>(istream& istr, newtime24& t)
{
char setparatChar;
istr >> t.hour >>setparatChar >> t.minute ;
t.normalizeTime();
return istr;
}
// 成员函数+=
newtime24& operator +=(const newtime24& rhs)
{
*this = *this +rhs;
return *this;
}
newtime24& operator +=(int min)
{
*this = *this + min;
return *this;
}
int getHour() const {return hour;}
int getMinute() const {return minute;}
private:
int hour,minute;
void normalizeTime()
{
int extraHours = minute / 60;
minute %= 60;
hour = (hour + extraHours ) % 24 ;
}
};
//测试的main
#include <iostream>
#include "newtime24.h"
using namespace std;
int main()
{
newtime24 apptTime, totalApptTime;
int apptLength;
int i;
for(i=1;i<=3;i++)
{
cout << "Enter start of appointment and length in minutes: " ;
cin >> apptTime >> apptLength;
cout << "Appointment " << i <<": start: " << apptTime
<< " stop: " << (apptTime + apptLength) << endl;
totalApptTime += apptLength;
}
cout << "Total appointment time: " << totalApptTime << endl;
return 0;
}
编译错误信息:
Compiling...
appt.Cpp
f:\simplycpp\数据结构\newtime24.h(32) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Error executing cl.exe.
appt.obj - 1 error(s), 0 warning(s)
好去掉头文件的-号重载,编译成功,但是+ += 2个重载运算符不起作用!
apptTime + apptLength =0??//错误
totalApptTime += apptLength ==0?? //错误!
但是输出输入>> <<又行!!
请问什么原因啊??i
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
class newtime24
{
public:
newtime24(int h=0, int m =0):hour(0),minute(0)
{
normalizeTime();
}
//一组+运算符 ++++++++++++++
friend newtime24 operator+(const newtime24& lhs, const newtime24& rhs)
{
return newtime24(lhs.hour+rhs.hour,lhs.minute+rhs.minute);
}
friend newtime24 operator+(const newtime24& lhs,int min)
{
return newtime24(lhs.hour,lhs.minute+min);
}
friend newtime24 operator+(int min, const newtime24& rhs)
{
return newtime24(rhs.hour,min+rhs.minute);
}
// - - - -- -
friend newtime24 operator- (const newtime24& lhs, const newtime24& rhs)
{
int currTime = lhs.hour*60 +lhs.minute;
int tTime = rhs.hour*60 + rhs.minute;
if(tTime < currTime)
throw string("time24 duration():argument is an earlier time");
else
return newtime24(0,tTime-currTime);
}
friend bool operator==(const newtime24& lhs, const newtime24& rhs)
{
return (lhs.hour*60 +lhs.minute)== ( rhs.hour*60 + rhs.minute);
}
//
friend bool operator<(const newtime24& lhs, const newtime24& rhs)
{
return (lhs.hour*60 +lhs.minute)<( rhs.hour*60 + rhs.minute);
}
// iostream
friend ostream& operator<< (ostream& ostr, const newtime24& t)
{
ostr << setfill('0') << t.hour << ":" << setw(2) << t.minute;
return ostr;
}
friend istream& operator>>(istream& istr, newtime24& t)
{
char setparatChar;
istr >> t.hour >>setparatChar >> t.minute ;
t.normalizeTime();
return istr;
}
// 成员函数+=
newtime24& operator +=(const newtime24& rhs)
{
*this = *this +rhs;
return *this;
}
newtime24& operator +=(int min)
{
*this = *this + min;
return *this;
}
int getHour() const {return hour;}
int getMinute() const {return minute;}
private:
int hour,minute;
void normalizeTime()
{
int extraHours = minute / 60;
minute %= 60;
hour = (hour + extraHours ) % 24 ;
}
};
//测试的main
#include <iostream>
#include "newtime24.h"
using namespace std;
int main()
{
newtime24 apptTime, totalApptTime;
int apptLength;
int i;
for(i=1;i<=3;i++)
{
cout << "Enter start of appointment and length in minutes: " ;
cin >> apptTime >> apptLength;
cout << "Appointment " << i <<": start: " << apptTime
<< " stop: " << (apptTime + apptLength) << endl;
totalApptTime += apptLength;
}
cout << "Total appointment time: " << totalApptTime << endl;
return 0;
}
编译错误信息:
Compiling...
appt.Cpp
f:\simplycpp\数据结构\newtime24.h(32) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Error executing cl.exe.
appt.obj - 1 error(s), 0 warning(s)
好去掉头文件的-号重载,编译成功,但是+ += 2个重载运算符不起作用!
apptTime + apptLength =0??//错误
totalApptTime += apptLength ==0?? //错误!
但是输出输入>> <<又行!!
请问什么原因啊??i
...全文
相关推荐
C++常用运算符重载简介
最近在学习C++运算符重载,这是我经过整理之后的一点资料,拿出来和大家分享,希望能对大家有所帮助。另外,选择C++开发的同志们,加油^_^一起努力
实验二 运算符重载的应用.doc
含实验心得,非常详细
C++ 类的赋值运算符”=”重载的方法实现
什么类需要重载赋值运算符 先来看一个普通类的直接赋值。 #include using namespace std; class person{ int age; public: person(const int& a=10):age(a){} //构造函数 ~person(); //析构函数 void showAdd(); //打印age的地址 }; person::~person(){cout<<析构\n;} void person::showAdd() {cout <<hex<< &age<<endl;} int main() { person a(1
运算符重载总结C++
函数类型 operator 运算符名称(形参列表) {对运算符重载的处理}
c++运算符重载.txt
1、什么是运算符重载? 对已有的运算符重新进行定义,赋予其另外一种功能,以适应不同的数据类型。 2、加号运算符重载。 对于int这种数据类型,加号+是懂得怎么加的,所以int的数据类型相加不会有问题。 对于Person这种数据类型,加号+是不懂得怎么加的,所以Person的数据类型相加就会编译出错。 如何告诉加号怎么去加Person数据类型的呢? --> 加号运算符重载


新手乐园
