gpt4 book ai didi

c++ - 如何将运算符重载为成员函数

转载 作者:行者123 更新时间:2023-11-30 04:52:29 25 4
gpt4 key购买 nike

<分区>

我的任务是将所有这些重载运算符从友元函数转换为成员函数。所以我继续删除 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;
}

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com