Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: ‘ostream’ does not name a type

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
like image 698
user2741941 Avatar asked Oct 21 '14 04:10

user2741941


Video Answer


2 Answers

Use std::ostream and std::istream everywhere.

ostream and istream are in namespace std

like image 127
P0W Avatar answered Oct 01 '22 21:10

P0W


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>

like image 43
Vlad from Moscow Avatar answered Oct 01 '22 19:10

Vlad from Moscow