我正在编写一个涉及使用复数的程序,因此我定义了一个复数类,该类重载了运算符,可以进行一个人可以进行的操作。他们已经在使用其他功能并对其进行了先前的测试,但是对于我的其中一项功能,我得到以下错误代码:
||=== 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)) ===|
很明显,在我的 header 中定义了一个运算符,它采用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;
}
我的问题是:我该如何解决这个问题,为什么会收到此错误消息?谢谢!
最佳答案
问题在于z^(deg-1)
返回的类型为Comp
的对象。该结果将传递给operator*
,后者接受Comp&
,即,它需要一个Comp
类型的左值; ^
返回一个临时对象,它是 rvalue 。解决方法是将operator*
(以及所有其他运算符)更改为const Comp&
。这样就可以用左值和右值调用它们。
关于c++ - 在已经定义运算符C++时不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48360898/