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;
}
This might seem like a strange error message. After all, one could fairly quickly come up with an example program that streams a
double
fromstd::cin
, which is astd::istream
. So what is wrong here?The answer comes in that mess of notes that follows this error message. (Yes, it can be an intimidating mess, but the notes are there to help diagnosing the problem. No, I do not expect the entire mess to be copied into the question, since it is large and most of it is not relevant.) Somewhere in the list of candidate operators is
This is the operator that allows streaming a
double
. However, note the type of the parameter – it is notdouble
, butdouble&
. The target of the stream must name a variable, not merely provide a value. In your case, attemptingin >> z.real()
is similar to attemptingin >> 3.1
. The types ofz.real()
and3.1
are bothdouble
, so what you can do to one you can do to the other. Hopefully you do not believe you can change mathematics by streaming a new value into3.1
. Similarly, you cannot stream a value into a function that returns adouble
.One solution is to make your function return what the stream operator expects, as in
double& real()
(add an ampersand and removeconst
). However, providing a public non-const reference to a private member destroys encapsulation; the member might as well be public at that point. Plus, your project does not allow it. Let's look for a better approach.A more common solution is to make
operator>>
afriend
of your class so that it can set the private members. This requires adding the lineto your class definition. After that is done, your implementation of
operator>>
can access the private data members, bypassing the accessor functions. Note: the definition ofoperator>>
can stay right where it is, outside the class definition, if that is desirable.A more roundabout approach uses construction and assignment instead of friendship. This might reduce code duplication in more complex cases. However, in your case it triggers a warning because your class violates the Rule of Three by having a copy constructor but no assignment operator. Still, your explicit copy constructor is what the compiler would automatically generate for you. You could address the warning by commenting out your copy constructor.
For something as simple as your
complex
class, I would go with friendship.