I am overloading the << and >> operator in C++, but it cannot compile.
The error message is :" error: ‘ostream’ does not name a type" Why do I got this error? How to fix it?
#ifndef COMPLEX_H
#define COMPLEX_H
#include <cstdlib> //exit
#include <istream>
#include <ostream>
class Complex{
public:
Complex(void);
Complex(double a, double b);
Complex(double a);
double real() const{
return a;
}
double imag() const{
return b;
}
friend ostream& operator<<(ostream& out,const Complex& c);
friend istream& operator>>(istream& in, Complex& c);
private:
double a;
double b;
};
ostream& operator<<(ostream& out,const Complex& c){
double a=c.real() , b = c.imag();
out << a << "+" << b<<"i";
return out;
}
istream& operator>>(istream& in, Complex& c){
in >> c.a>> c.b;
return in;
}
#endif
Use std::ostream
and std::istream
everywhere.
ostream
and istream
are in namespace std
Us qualified names for types defined in namespace std
friend std::ostream& operator<<(std::ostream& out,const Complex& c);
It would be also better to include <iostream>
instead of two separate headers <istream>
and <ostream>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With