重载operator时出现的问题,同道帮忙看看吧。
重载+以使如下命令运行#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+以友元声明
编译时报错,现在脑子有点僵,想不出问题出在哪儿。。$郁闷$ 出错信息怎么不贴 Natural::Natural(int Value)
{
int Wert = Value;
}
构造函数里的Wert成员变量有重定义错误。
Natural::Natural(const Natural &n)
{
int Wert= Value;
}
复制构造函数里面的参数n根本就没用上。Wert成员变量还是重定义错误。
昏$郁闷$
页:
[1]