重载一组运算符出错请高手帮忙找原因!(奇怪的错误信息)
头文件:
#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?? //错误!
但是输出输入>> <<又行!!
请问什么原因啊??
问题点数:50、回复次数:5Top
1 楼wuyinggu(寂寞小阳春)回复于 2006-05-01 22:35:04 得分 0
newtime24.h(32),就是-号重载出错,去掉后编译可以成功Top
2 楼du51(郁郁思扬)回复于 2006-05-02 00:17:46 得分 15
你代码里有全角.Top
3 楼wanfustudio(雁南飞:知识之败,慕虚名而不务潜修也)回复于 2006-05-02 09:01:34 得分 15
符号问题~
注意输入法~Top
4 楼wuyinggu(寂寞小阳春)回复于 2006-05-02 19:54:10 得分 0
不是这个问题,试了去改了也不行哦Top
5 楼braveconf()回复于 2006-05-02 20:26:27 得分 20
什么编译器?
在vc7中没有问题。vc6对友员支持不好,所以经常有怪异出现,有补丁,打了也可以Top




