2017-11-06 5 views
0

時間内にaddminutesにフレンド機能を書き込んで、それに応じて時間を変更しようとしています 例:時刻t3(11,58,0) addMinutes(t3,10); t3.print()は12:08:00 PMになるはずです は私がコンパイルしようとしているときに '分'が 'Time'の私的メンバーであることを示すコードです。ここでやった。フレンド機能プライベートデータメンバーにアクセスできない

/*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(); 
} 
+2

フレンド宣言と定義のプロトタイプを注意深く読んでください。 – molbdnilo

+1

const参照で 'int'のようなプリミティブを渡さないでください。それは無意味な悲嘆です。 – molbdnilo

答えて

0

あなたがして、あなたのフレンド関数を宣言:

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

しかしでそれを定義します。

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

変更上記に:

friend void addMinutes(Time &, int); 

したがって、コンパイルします。

関連する問題