1

I was given the assignment to convert all of these overloaded operators from friends to member functions. So I went ahead and removed the friend and added the member functions as I was taught but when I go to compile I get an error that says. "error: '' must take either zero or one argument"

This is the original code: Header File:

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

Class File:

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

This is what I have to get rid of the friend tags. Im not sure what the error is but it is on all of the operators.

Header File:

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

Class File:

// 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;
}
  • Hi, welcome to StackOverflow. Please, before posting, read [ask] and post a [mcve] of the exact problem you've encountered. Let me also remind you that StackOverflow isn't a homework solving platform. – ProXicT Jan 24 '19 at 13:53
  • Friend operator is a global function, so it must take both parameters. Member operator is a member function, so it already has one "parameter", which is the object itself (accessed with `this` keyword). So member operator needs only one parameter, such that `this` stands for the left hand side of the operator, and the parameter stands for the right hand side. – Dialecticus Jan 24 '19 at 13:55
  • 1
    @ProXicT _"Let me also remind you that StackOverflow isn't a homework solving platform."_ I don't know where you got that from. A well asked and explained question will be accepted and answered here, no matter if its homework or a real world problem. – πάντα ῥεῖ Jan 24 '19 at 13:58
  • @Dialecticus I thought thats what the problem was so I went ahead a tried removing the second parameter from all of the functions but then the compiler didnt recognize them anymore. It just said no function defined. – Justin Levy Jan 24 '19 at 14:03
  • We don't see that in provided code, so we can't tell what is the problem. Create another question with just one operator (reduce the example to minimum, but still complete enough that we can copy it and compile it), and this one operator has just one parameter (that stands for right-hand side of the operator). Also provide output of the compiler. – Dialecticus Jan 24 '19 at 14:20
  • @πάνταῥεῖ Right, I stated that wrong, what I meant was that I couldn't see much effort of trying to solve the problem on his own. There is a ton of answers about this particular problem he faces, if he read a single one of them, he'd definitely solved it himself. Also, [this is a good "cheat-sheet"](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) on how to ask homework questions. – ProXicT Jan 25 '19 at 02:45

1 Answers1

1

First argument of your friend methods would be *this, so transform

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

into

class MyTime
{
public:
    MyTime operator - (const MyTime& other) const;
    // ...
};
Jarod42
  • 203,559
  • 14
  • 181
  • 302
  • I tried that at the start. But then I was getting this error in the compiler. "error: no 'MyTime MyTime::operator+(const MyTime&)' member function declared in class 'MyTime' MyTime MyTime::operator + (const MyTime& t1) {" – Justin Levy Jan 24 '19 at 14:10
  • @JustinLevy That's a completely different problem as you asked for. Ask another question providing a [mcve] that reproduces your problem as required please. – πάντα ῥεῖ Jan 24 '19 at 14:16