tags:

views:

49

answers:

3

When I try to compile my program the compiler complains about this line in a .h file that I #included.

ostream & Print (ostream & stream);

How can this be fixed?

+5  A: 

If you #include <ostream>, ostream will be defined in the std namespace:

#include <ostream>

// ...

std::ostream & Print (std::ostream & stream);
GMan
If your program's not enormous, just put 'using std::ostream' after the #include ; life and lines are both too short to type std:: more than you have to
Tom Womack
Better may be #include <iosfwd>, that contains only forward declarations.
Matteo Italia
@Tom Womack: NEVER EVER put using statements in a header file. Life is way to short to sort out the trouble that that will cause when it blows up in your face.
Martin York
A: 

Minimal code for this declaration to compile:

#include <iosfwd>
using namespace std;
BennyG
You shouldn't put `using namespace std;` in a header file.
Yacoby
Agreed. But I didn't say this code was going into a header file. If you don't have control over the contents of the header file being included, these two lines will get it working from the .cpp file.Regardless, <iosfwd> is a better choice than <ostream> in this case.
BennyG
A: 

Use 'using' if you don't want to pull the whole std namespace, eg :

#include <iosfwd>
using std::ostream;
OneOfOne