For my project I'm trying to create a free function for a complex number class. It is defined in the cpp file. The function is an overloaded input streaming operator but I keep getting the error
No operator ">>" matches these operands operand types are: std::istream >> double
on the lines
in >> z.real();
in >> z.imag();
I made a file called complex.h
that contains the complex
class and the two overloaded operators I want to work, constructors for complex numbers (not sure if needed but included), and two getter methods to retrieve the real and imaginary parts of the complex class. This reproduces the error.
The declarations of the member functions are dictated by my project spec. Their return types cannot be changed.
#pragma once
#include <iostream>
#include <cmath>
class complex {
private://may include private helper functions
double realX = 0;
double imaginaryY = 0;
public:// interface for operators and member functions (methods)
//**********************Constructors***************************
complex() {}
complex(double x) {
realX = x;
}
complex(double x, double y) {
realX = x;
imaginaryY = y;
}
complex(const complex& z) : realX(z.realX), imaginaryY(z.imaginaryY) { //copy constructor
}
double real() const {
return realX;
}
double imag() const {
return imaginaryY;
}
};
std::istream& operator>>(std::istream& in, complex& z) {
in >> z.real();
in >> z.imag();
return in;
}
std::ostream& operator<<(std::ostream& output, const complex& z) {
output << "(" << z.real()
<< ", " << z.imag()
<< "i)";
return output;
}