作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
<分区>
我的任务是将所有这些重载运算符从友元函数转换为成员函数。所以我继续删除 friend 并按照我所教的那样添加成员函数但是当我去编译时我得到一个错误说。 “错误:'' 必须采用零或一个参数”
这是原始代码:头文件:
#include <iostream>
class MyTime
{
public:
// CONVERT THESE CONSTRUCTORS INTO A SINGLE CONSTRUCTOR THAT USES
// DEFAULT ARGUMENTS
MyTime();
MyTime(int h, int m);
void Reset(int h, int m);
MyTime operator + (const MyTime& t1);
friend MyTime operator - (const MyTime& t1, const MyTime& t2);
friend MyTime operator * (const MyTime& t1, int num);
friend MyTime operator / (const MyTime& t1, int num);
friend std::istream& operator >>(std::istream& fin, MyTime& t);
friend std::ostream& operator <<(std::ostream& fout, const MyTime& t);
friend bool operator == (const MyTime& t1, const MyTime& t2);
friend bool operator < (const MyTime& t1, const MyTime& t2);
friend bool operator <= (const MyTime& t1, const MyTime& t2);
void input(std::istream& ins);
void output(std::ostream& outs);
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
};
类文件:
// The implementation file for the MyTime class
#include "MyTime.h"
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
// Constructors
MyTime::MyTime(){
hours = 0;
minutes = 0;
}
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;
}
MyTime operator + (const MyTime& t1, const MyTime& t2){
MyTime tmp;
tmp.hours = t1.hours + t2.hours;
tmp.minutes = t1.minutes + t2.minutes;
tmp.simplify();
return tmp;
}
MyTime operator - (const MyTime& t1, const MyTime& t2){
MyTime tmp;
tmp.minutes = abs((t1.hours*60+t1.minutes) -
(t2.hours*60+t2.minutes));
tmp.simplify();
return tmp;
}
MyTime operator / (const MyTime& t1, int num){
MyTime tmp;
tmp.minutes = t1.hours*60 + t1.minutes;
tmp.minutes /= num;
tmp.simplify();
return tmp;
}
MyTime operator * (const MyTime& t1, int num){
MyTime tmp;
tmp.minutes = t1.hours*60 + t1.minutes;
tmp.minutes *= num;
tmp.simplify();
return tmp;
}
bool operator == (const MyTime& t1, const MyTime& t2){
return t1.hours == t2.hours && t1.minutes == t2.minutes;
}
bool operator < (const MyTime& t1, const MyTime& t2){
return (t1.hours*60 + t1.minutes) < (t2.hours*60 + t2.minutes);
}
bool operator <=(const MyTime& t1, const MyTime& t2){
return (t1.hours*60 + t1.minutes) <= (t2.hours*60 + t2.minutes);
}
void MyTime::input(istream&ins){
// Here you are to copy the implementation code from the >> operator shown below
// remember to that variables will be local here - so no referene to t1
//Then you can have the >> operator call this function.
// In the .h file remove the word friend for the operator and move its prototype to a spot
// under the class declaration
}
void MyTime::output(ostream& outs){
// Do the same thing a you did for the function above except using the code for the <<
//operator
}
ostream& operator <<(ostream& outs, const MyTime& t1){
outs<<t1.hours<<':'<<setw(2)<<setfill('0')<<t1.minutes;
return outs;
}
istream& operator >> (istream& ins, MyTime& t1){
char junk;
ins>>t1.hours;
ins.get(junk);
ins>>t1.minutes;
t1.simplify();
return ins;
}
这就是我必须摆脱 friend 标签的原因。我不确定错误是什么,但它存在于所有运算符(operator)身上。
头文件:
// need doucumentation for each member function - similar to your last
// project
#include <iostream>
class MyTime
{
public:
// CONVERT THESE CONSTRUCTORS INTO A SINGLE CONSTRUCTOR THAT USES
// DEFAULT ARGUMENTS
MyTime();
MyTime(int h, int m);
void Reset(int h, int m);
MyTime operator + (const MyTime& t1, const MyTime& t2);
MyTime operator - (const MyTime& t1, const MyTime& t2);
MyTime operator * (const MyTime& t1, int num);
MyTime operator / (const MyTime& t1, int num);
std::istream& operator >>(std::istream& fin, MyTime& t);
std::ostream& operator <<(std::ostream& fout, const MyTime& t);
bool operator == (const MyTime& t1, const MyTime& t2);
bool operator < (const MyTime& t1, const MyTime& t2);
bool operator <= (const MyTime& t1, const MyTime& t2);
void input(std::istream& ins);
void output(std::ostream& outs);
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
};
类文件:
// The implementation file for the MyTime class
#include "MyTime.h"
#include <cstdlib>
#include <iostream>
#include <iomanip>
using namespace std;
// Constructors
MyTime::MyTime(){
hours = 0;
minutes = 0;
}
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;
}
MyTime MyTime::operator + (const MyTime& t1, const MyTime& t2) {
MyTime tmp;
tmp.hours = t1.hours + hours;
tmp.minutes = t1.minutes + minutes;
tmp.simplify();
return tmp;
}
MyTime MyTime::operator - (const MyTime& t1, const MyTime& t2){
MyTime tmp;
tmp.minutes = abs((t1.hours*60+t1.minutes) -
(t2.hours*60+t2.minutes));
tmp.simplify();
return tmp;
}
MyTime MyTime::operator / (const MyTime& t1, int num){
MyTime tmp;
tmp.minutes = t1.hours*60 + t1.minutes;
tmp.minutes /= num;
tmp.simplify();
return tmp;
}
MyTime MyTime::operator * (const MyTime& t1, int num){
MyTime tmp;
tmp.minutes = t1.hours*60 + t1.minutes;
tmp.minutes *= num;
tmp.simplify();
return tmp;
}
bool MyTime::operator == (const MyTime& t1, const MyTime& t2){
return t1.hours == t2.hours && t1.minutes == t2.minutes;
}
bool MyTime::operator < (const MyTime& t1, const MyTime& t2){
return (t1.hours*60 + t1.minutes) < (t2.hours*60 + t2.minutes);
}
bool MyTime::operator <=(const MyTime& t1, const MyTime& t2){
return (t1.hours*60 + t1.minutes) <= (t2.hours*60 + t2.minutes);
}
void MyTime::input(istream&ins){
// Here you are to copy the implementation code from the >> operator shown below
// remember to that variables will be local here - so no referene to t1
//Then you can have the >> operator call this function.
// In the .h file remove the word friend for the operator and move its prototype to a spot
// under the class declaration
}
void MyTime::output(ostream& outs){
// Do the same thing a you did for the function above except using the code for the <<
//operator
}
ostream& operator <<(ostream& outs, const MyTime& t1){
outs<<t1.hours<<':'<<setw(2)<<setfill('0')<<t1.minutes;
return outs;
}
istream& operator >> (istream& ins, MyTime& t1){
char junk;
ins>>t1.hours;
ins.get(junk);
ins>>t1.minutes;
t1.simplify();
return ins;
}
我是一名优秀的程序员,十分优秀!