The name ostream
is located in the std
-namespace, so you need to introduce that name. The least intrusive way is to just qualify it explicitly:
friend std::ostream& operator<< ....
^^^
Another way is using a using
- or using namespace
-directive. They allow you to import the name/s to the rest of the translation unit:
using std::ostream; // cherry-pick the names
friend ostream& operator<< ....
or
using namespace std; // fire a shotgun with a huge and growing bunch of names
friend ostream& operator<< ....
These have advantages and disadvantages:
- The name becomes shorter and might be better readable in the context
- Name-Clashes may arise when other namespaces define the same names (consider e.g.
std::pow
vs.awesome_math_lib::pow
).
An agreed upon rule-of-thumb for good C++-code is to never use using
or using namespace
in the global namespace in an header file, and with care in source files.
Many also agree that std::
is so short and standard, that they never use using
or using namespace
on it (except within functions) and just stick to typing std::...
.