错误代码:一元表达式的参数类型“Complexno”无效

Error code: invalid argument type 'Complexno' to unary expression

提问人:Gab 提问时间:6/13/2022 更新时间:6/13/2022 访问量:648

问:

我正在制作一个程序来重载一元 -、+、-、*、/、<<、运算符。每次我运行它时,我都会收到此错误:  使 -s 驱动程序.cpp:63:18:错误:一元表达式的参数类型“Complexno”无效 n3 = -n1; ^~~ 生成 1 个错误。 make: *** [Makefile:17: driver.o] 错误 1 退出状态 2

我的 .h文件

#ifndef COMPLEXNUM_H
#define COMPLEXNUM_H
#include <iostream>
#include <cmath>
using namespace std;

class Complexno {
public :
Complexno(); // Default constructor
Complexno(double r); // Second constructor - creates a complex number of equal value to a real.
Complexno(double r, double c); // (1) Standard constructor - sets both of the real and complex

friend Complexno operator + (Complexno,Complexno);
friend Complexno operator * (Complexno,Complexno);
friend Complexno operator - (Complexno,Complexno);
friend Complexno operator / (Complexno,Complexno);
friend ostream& operator<<(ostream &,Complexno &);

double magnitude(); // (4) Computes and returns the magnitude of a complex number.

void shownum(); // (5) Prints out a complex number in a readable format.

Complexno negate(); // Negates a complex number.
void enternum(); // Reads in a complex number from the user.

private :
double real; // Stores real component of complex number
double complex; // Stores complex component of complex number
};

// Displays the answer to a complex number operation.
void display(Complexno, Complexno, Complexno, char);
#endif 

my.cpp 文件

#include <cmath>
#include "complexnum.h"

// Default constructor sets both components to 0.
Complexno::Complexno() {
real = 0.0;
complex = 0.0;
}

// Second constructor - creates a complex number of equal value to a real.
Complexno::Complexno(double r) {
real = r;
complex = 0.0;
}

// (1) --------------------------------- standard constructor ------------------

//Define standard constructor - sets both of the real and complex components based
Complexno::Complexno(double r, double c) {
real = r;
complex = c;
}

//................overloaded operator + ................

// Adds two complex numbers and returns the answer.
Complexno operator + (Complexno num1,Complexno num2) {
Complexno answer;
answer.real = num1.real + num2.real;
answer.complex = num1.complex + num2.complex;
return answer;
}

// (2) --------------------------------- overloaded operator - ------------------

// Define sub to subtracts two complex numbers and returns the answer.
Complexno operator - (Complexno num1,Complexno num2) {
Complexno answer;
answer.real = num1.real - num2.real;
answer.complex = num1.complex - num2.complex;
return answer;
}

// (3) --------------------------------- overloaded operator * ------------------

// Multiplies two complex numbers and returns this answer.

Complexno operator * (Complexno num1,Complexno num2) {
Complexno answer;
answer.real = num1.real * num2.real - num1.complex * num2.complex;
answer.complex = num1.real * num2.complex + num1.complex * num2.real;
return answer;
}

// --------------------------------- overloaded operator /------------------

// Division two complex numbers and returns this answer.

Complexno operator / (Complexno num1,Complexno num2) {
Complexno answer;
answer.real = (num1.real * num2.real + num1.complex * num2.complex)/(num2.real * num2.real + num2.complex * num2.complex);
answer.complex = (num1.real * num2.real - num1.complex * num2.complex)/(num2.real * num2.real + num2.complex * num2.complex);
return answer;
}

// --------------------------------- overloaded operator << ------------------

ostream& operator<<(ostream &out,Complexno &C)

{
out<<"(";
out<<C.real;
out<<"+";
out<<C.complex<<"i)";
return out;
}

// Negates a complex number.
Complexno Complexno::negate() {
Complexno answer;
answer.real = -real;
answer.complex = -complex;
return answer;
}

// (4) --------------------------------- Magnitude ------------------

// Computes and returns the magnitude of a complex number.

double Complexno::magnitude() {
double answer;
answer = sqrt(real * real + complex * complex);
return answer;
}

// (5) --------------------------------- Print ------------------

// Prints out a complex number in a readable format.

void Complexno::shownum() {
if(complex == 0)
{
cout << "(" << real << ")";
}
else{
if(complex > 0)
cout << "(" << real << "+" << complex << "i" << ")";
else
cout << "(" << real << complex << "i" << ")";
}
}

// Reads in a complex number from the user.
void Complexno::enternum() {
cout << "Enter the real part of the complex number : ";
cin >> real;
cout << "Enter the imaginary part of the complex number : ";
cin >> complex;
}

// Displays the answer to a complex number operation.

void display(Complexno n1, Complexno n2, Complexno n3, char op) {
n1.shownum();
cout << " " << op << " ";
n2.shownum();
cout << " = ";
n3.shownum();
cout << endl;
}  
c++ 重载 一元运算符

评论

2赞 Quimby 6/13/2022
我也没有看到任何一元运算符减号,所以编译器似乎是正确的。我还建议您使用运算符的参数类型。const Complexno&
1赞 molbdnilo 6/13/2022
请阅读最小可重现示例。您只需要该代码的一小部分来说明该问题。
0赞 Gab 6/14/2022
谢谢 molbdnilo,我会在以后的帖子中牢记这一点!

答:

0赞 Tarik Ibrahimović 6/13/2022 #1

具有一元含义,它只需要一个参数,您的重载“-”运算符确实不是一元的,因此 n3 = -n1 是不可能的。对于这样的事情,你可以声明一个朋友函数,并像这样实现它:friend Complexno operator-(const Complexno &z)

return Complexno(-z.real,-z.complex);

或类中的 const 方法

Complexno operator-() const{return Complexno(-real,-complex);}

评论

1赞 Gab 6/14/2022
谢谢!经过研究,我最终做了完全相同的事情,感谢您的帮助!