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();
}
フレンド宣言と定義のプロトタイプを注意深く読んでください。 – molbdnilo
const参照で 'int'のようなプリミティブを渡さないでください。それは無意味な悲嘆です。 – molbdnilo