所以我想重载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。任何帮助,将不胜感激。
2月12日加5月30日是什么? – 2013-05-08 22:57:51
4月1日@ @ KerrekSB – 2013-05-08 23:28:01
@Kerrek:假设我们的约会系统在接下来的138亿年左右保持一致,大约在今年1月10日13797295634. – 2013-05-08 23:35:47