0

I am trying to write friend function to addminutes in time and then change hour accordingly Eg: if Time t3(11,58,0) addMinutes(t3,10); t3.print() should give 12:08:00 PM below is the code which when I am trying to compile is giving 'minute' is a private member of 'Time'.Please let me know what function defining error I have done here.

/*Header File for Time */
Time.h

#ifndef RATIONALNO_TIME_H
#define RATIONALNO_TIME_H
#include<iostream>
using namespace std;
class Time{
    friend void addMinutes(Time&,const int&);
public:
    Time(const int&,const int&,const int&);
    void print()const;
private:
    int hour;
    int minute;
    int second;
};
#endif //RATIONALNO_TIME_H

/*Source File*/
Time.cc


#include "Time.h"
Time::Time(const int& h,const int& m,const int& s)
{
    if(h>=0 && h<=23) {
        hour = h;
    }else
        hour=0;
    if(m>=0 && m<=59) {

        minute = m;
    }else
        minute=0;
    if(s>=0 && s<=59) {
        second = s;
    }else second=0;
}

void Time::print() const {

    cout<<hour;
    cout<<minute;
    cout<<second<<endl;
    cout<<((hour==0||hour==12)?12:hour%12)<<":"<<minute<<":"<<second<<(hour<12?"AM":"PM")<<endl;
}

void addMinutes(Time& time1,int & m)
{
    int hr;
   if(time1.minute+m>59)
   {
       hr=time1.hour+1;
   }
}

int main()
{
    Time t1(17,34,25);
    t1.print();

    Time t3(11,58,0);
    t3.print();
    addMinutes(t3,10);
    t3.print();
}

1 Answers1

0

You declare your friend function with:

friend void addMinutes(Time&,const int&); // (Time&, const int&) <- const int

But define it with:

void addMinutes(Time& time1,int & m)... // (Time& time1, int & m) <- non-const int

Change the above to:

friend void addMinutes(Time &, int);

and

void addMinutes(Time &time1, int m)....

accordingly, and it compiles.

Killzone Kid
  • 6,171
  • 3
  • 17
  • 37