|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
重载+以使如下命令运行
#include <iostream>
#include <iomanip>
#include "Natural.h"
#include <cassert>
#include <climits>
using namespace std;
void main()
{
int value1 = 9992, value2 = 9;
Natural v1(value1),v2(value2);
cout << "Testen von Natural::operator+" << endl;
cout << value1 << " + " << value2 << " = " << (v1 + v2).getValue() << endl;
cout << value1 << " + " << value2 << " = " << (v1 + value2).getValue() << endl;
cout << value1 << " + " << value2 << " = " << (value1 + v2).getValue() << endl << endl;
assert( v1 == Natural(value1) );
assert( v2 == Natural(value2) );
assert( v1+v2 == Natural(value1+value2) );
}
这里是我写的
#include "Natural.h"
#include <iostream>
using namespace std;
Natural::Natural(int Value)
{
int Wert = Value;
}
Natural::Natural(const Natural &n)
{
int Wert= Value;
}
int Natural::getValue() const// calculate time in seconds since midnight
{
int payBack;
payBack = Wert;
return payBack;
}
int operator+(const Natural &n) const
{
return Natural( getValue()
+ n.getValue() );
}
在head文件里operator+以友元声明
编译时报错,现在脑子有点僵,想不出问题出在哪儿。。$郁闷$ |
|