The identifier "ostream" is an undefined error

I need to implement a class of numbers that supports the <<operator for output. I have a mistake: "The identifier" ostream "is undefined" for some reason, although I turned it on and tried also

here is 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 doesn't the compiler recognize ostream in a friend function?

+6
source share
2 answers

You need to fully define the name ostream with the namespace name in which this class lives:

  std::ostream // ^^^^^ 

So, your statement of the operator should become:

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

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

 using std::ostream; 

This will allow you to write the name ostream without full qualifications, as in the current version of the program.

+11
source

Andy Prowl's answer is great, but please mind "using std :: ostream" in the header. If you do this, other compilation units using your header file will also have this default namespace, and you may get nasty compilation errors with namespace conflicts.

0
source

Source: https://habr.com/ru/post/944963/


All Articles