我正在完成任务,并在特定区域撞墙。我无法弄清楚我应该如何将标头中的非成员函数实现到.cpp文件中。这是标题:
class complx
{
double real, imag;
public:
complx( double real = 0., double imag = 0.); // constructor
complx operator+(complx); // operator+()
complx operator+(double); // operator+()with double
complx operator- (complx); // operator-()
complx operator* (complx); // operator*()
bool operator== (complx); // operator==()
//Sets private data members.
void Set(double new_real, double new_imaginary) {
real = new_real;
imag = new_imaginary;
}
//Returns the real part of the complex number.
double Real() {
return real;
}
//Returns the imaginary part of the complex number.
double Imaginary() {
return imag;
}
};
ostream &operator << ( ostream &out_file, complx number );
extern istream &operator >> ( istream &in_file, complx &number );
extern ifstream &operator >> ( ifstream &in_file, complx &number );
complx &operator + (double, complx);
complx &operator - (double, complx);
complx &operator * (double, complx);
}
我已经找到了大部分成员函数,但它是标题底部的三个让我适合的。任何帮助将不胜感激。谢谢!
P.S。抱歉格式化,它不是很好地复制。
答案 0 :(得分:0)
在你的.ccp文件中:
ostream &operator << ( ostream &out_file, complx number )
{
out_file << number.Real() << "," << number.Imaginary();
return out_file;
}
与其他人类似。
诀窍是将复数分解为其元素(Real和Imaginary)并对其执行所需的操作(并为+
,-
的返回值创建一个新的复杂对象, *
)。