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

722 Views Asked by At

I'm making a program to overload unary -, +, -, *, /, <<, operators. Everytime I run it, I keep getting this error:  make -s driver.cpp:63:18: error: invalid argument type 'Complexno' to unary expression n3 = -n1; ^~~ 1 error generated. make: *** [Makefile:17: driver.o] Error 1 exit status 2

my .hfile

#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 file

#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;
}  
1

There are 1 best solutions below

1
Tarik Ibrahimović On

With unary meaning, it only takes one parameter, your overloaded '-' operator is truly not unary, therefore n3 = -n1 is not possible. For something like that you could declare a friend function friend Complexno operator-(const Complexno &z) and implement it like this:

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

or a const method inside the class

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