如何重载运算符作为成员函数

时间:2019-01-24 13:50:10

标签: c++

我被分配了将所有这些重载运算符从朋友转换为成员函数的任务。因此,我继续按照我的教导去掉了朋友并添加了成员​​函数,但是当我进行编译时,出现了一条错误消息。 “错误:”必须接受零或一个参数”

这是原始代码: 头文件:

#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;
}

这是我必须摆脱的朋友标签。我不确定错误是什么,但它在所有运算符上都有。

头文件:

// 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;
}

1 个答案:

答案 0 :(得分:1)

您的friend方法的第一个参数为*this,因此变换

class MyTime
{
public:
    friend MyTime operator - (const MyTime& t1, const MyTime& t2);
    // ...
};

进入

class MyTime
{
public:
    MyTime operator - (const MyTime& other) const;
    // ...
};