user2741941
user2741941

Reputation: 343

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

Upvotes: 22

Views: 70446

Answers (3)

Lucca Psaila
Lucca Psaila

Reputation: 51

You forgot to add

using namespace std;

Upvotes: 5

P0W
P0W

Reputation: 47844

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

ostream and istream are in namespace std

Upvotes: 42

Vlad from Moscow
Vlad from Moscow

Reputation: 311126

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>

Upvotes: 7

Related Questions