identifier "ostream" is undefined error

Aviad Chmelnik picture Aviad Chmelnik · May 14, 2013 · Viewed 12.6k times · Source

i need to implement a number class that support operator << for output. i have an error: "identifier "ostream" is undefined" from some reason eventhough i included and try also

here the header file:

Number.h

#ifndef NUMBER_H
#define NUMBER_H
#include <iostream>
class Number{
public:
//an output method (for all type inheritance from number):
virtual void show()=0;

//an output operator:
friend ostream& operator << (ostream &os, const Number &f);


};

#endif

why the compiler isnt recognize ostream in the friend function?

Answer

Andy Prowl picture Andy Prowl · May 14, 2013

You need to fully qualify the name ostream with the name of the namespace that class lives in:

    std::ostream
//  ^^^^^

So your operator declaration should become:

friend std::ostream& operator << (std::ostream &os, const Number &f);
//     ^^^^^                      ^^^^^

Alternatively, you could have a using declaration before the unqualified name ostream appears:

using std::ostream;

This would allow you to write the ostream name without full qualification, as in your current version of the program.