我正在编写一个涉及使用复数的程序,所以我已经定义了一个复数的数字类,它为一个人可以拥有的操作重载了运算符。他们已经使用其他函数和先前的测试但我的一个函数我得到以下错误代码:
||=== Build: Debug in complex (compiler: GNU GCC Compiler) ===|
/home/gagler/c_stuff/complex/polynomials.h||In function ‘Comp f(Comp*, int, Comp)’:|
/home/gagler/c_stuff/complex/polynomials.h|81|error: no match for ‘operator*’ (operand types are ‘Comp’ and ‘Comp’)|
/home/gagler/c_stuff/complex/polynomials.h|81|note: candidates are:|
/home/gagler/c_stuff/complex/complex.h|60|note: Comp Comp::operator*(double)|
/home/gagler/c_stuff/complex/complex.h|60|note: no known conversion for argument 1 from ‘Comp’ to ‘double’|
/home/gagler/c_stuff/complex/complex.h|68|note: Comp Comp::operator*(Comp&)|
/home/gagler/c_stuff/complex/complex.h|68|note: no known conversion for argument 1 from ‘Comp’ to ‘Comp&’|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
在我的标题中明确地定义了一个运算符,其中包含Comp和Comp的操作数。 Comp是我定义的类。 以下是带有运算符定义的头文件:
#ifndef COMPLEX_H
#define COMPLEX_H
#define PI 3.1415926535
#include <math.h>
#include "combinatorics.h"
double abs(double num){
return (num < 0) ? num * -1 : num;
}
class Comp{
public:
double re;//Re(z)
double im;//Im(z)
Comp(double a, double b){
re = a;
im = b;
}
Comp(){
re = 0;
im = 0;
}
double arg(){
double theta = atan(abs(im) / abs(re));
if(re < 0){
theta = PI - theta;
}
if(im < 0){
theta *= -1;
}
return theta;
}
double mod(){
return sqrt( re * re + im * im);
}
Comp conj(){
Comp z1(re, im*-1);
return z1;
}
Comp operator+(double a){
Comp z(this->re + a, this->im);
return z;
}
void operator+=(double a){
this->re += a;
}
Comp operator+(Comp &z1){
Comp z2(this->re + z1.re, this->im + z1.im);
return z2;
}
void operator+=(Comp &z){
this->re += z.re;
this->im += z.im;
}
Comp operator*(double scalar){
Comp z(this->re * scalar, this->im * scalar);
return z;
}
void operator*=(double scalar){
this->re *= scalar;
this->im *= scalar;
}
Comp operator*(Comp &z1){
Comp z2(this->re * z1.re - this->im * z1.im, this->re * z1.im + this->im * z1.re);
return z2;
}
void operator*=(Comp &z1){
//this->re = this->re * z1.re - this->im * z1.im;
//this->im = this->re * z1.im + this->im * z1.re;
Comp z2(this->re * z1.re - this->im * z1.im, this->re * z1.im + this->im * z1.re);
*this = z2;
}
Comp operator^(int exp){
Comp z;
for(int i = 0; i <= exp; i++){
double term = ncr(exp, exp - i) * pow(this->re, exp - i) * pow(this->im, i);
switch(i % 4){
case(0):{
z.re += term;
break;
}
case(1):{
z.im += term;
break;
}
case(2):{
z.re -= term;
break;
}
case(3):{
z.im -= term;
break;
}
}
}
return z;
}
Comp operator/(double scalar){
Comp z(this->re/scalar, this->im/scalar);
return z;
}
void operator/=(double scalar){
this->re = this->re/scalar;
this->im = this->im/scalar;
}
Comp operator/(Comp &z1){
Comp z2 = z1.conj();
z2 = (z2 * (*this)) / (z2.re * z2.re + z2.im * z2.im);
return z2;
}
void operator/=(Comp &z1){
Comp z2 = z1.conj();
*this = (z2 * (*this)) / (z2.re * z2.re + z2.im * z2.im);
}
};
#endif //COMPLEX_H
这里是代码失败的函数,它基本上只是一个函数,它计算给定复数'z'的f(z)= ax ^ n + bx ^(n-1)+ ... + k:
Comp f(Comp *poly, int deg, Comp z){
Comp y;
for(int i = 0; i <= deg; i++){
//here is the use of the overloaded operators
y += (poly[i])*( z^(deg-i) );
}
return y;
}
我的问题是:我该如何解决这个问题?为什么我会收到此错误?谢谢!
答案 0 :(得分:0)
问题是z^(deg-1)
返回Comp
类型的对象。该结果将传递给operator*
Comp&
,即需要Comp
类型的左值; ^
返回一个临时对象,即右值。修复方法是更改operator*
(以及所有其他运算符)以const Comp&
。这样就可以用左值和右值来调用它们。
答案 1 :(得分:0)
这很可能是因为表达式( z^(deg-i) )
是临时 Comp
对象,而且这些对象可以绑定到非常量引用。
您需要进行所有运算符参数const
引用。像
Comp operator*(Comp const &z1){