だから私はオーバーロードしたいoperator+
。ここに私がこれまでに持っているものがありますが、まだ機能していません。構文を書くにはどうすればよいですか?ヘッダー ファイル:
private:
int month;
int year;
int day;
public:
upDate();
upDate(int M, int D, int Y);
void setDate(int M, int D, int Y);
int getMonth();
int getDay();
int getYear();
int getDateCount();
string getMonthName();
friend upDate operator+(const upDate &lhs, const upDate &rhs);
私の .cpp ファイル
upDate::upDate()
{
month = 12;
day = 12;
year = 1999;
}
upDate::upDate(int M, int D, int Y)
{
month = M;
day = D;
year = Y;
}//end constructor
void upDate::setDate(int M, int D, int Y)
{
month = M;
day = D;
year = Y;
}//end setDate
int upDate::getMonth()
{
return month;
}//end get Month
int upDate::getDay()
{
return day;
}//end getDate
int upDate::getYear()
{
return year;
}//end getYear
upDate operator+(const upDate &lhs, const upDate &rhs)
{
upDate temp;
temp.day = lhs.day + rhs.day;
return (temp);
}
私の主に私が持っている
upDate D1(10,10,2010);//CONSTRUCTOR
upDate D2(D1);//copy constructor
upDate D3 = D2 + 5;//add 5 days to D2
upDate D4 = 5 + D2;// add 5 days to D2
エラーは、オブジェクトを int に追加できないことです。私はそれが機能する方法を試しましたが、D3 = D2 + 5 でのみ機能し、D4 では機能しませんでした。どんな助けでも大歓迎です。