2

This code compiles in VS2010, and I believe it does for any compiler.

#include <iosfwd>
using namespace std;
class ostream;
int main() {}

The same happens with this code

#include <iosfwd>
using namespace std;
int main() { class ostream; }

But this code generates error C2872: 'ostream' : ambiguous symbol

#include <iosfwd>
using namespace std;
class ostream;
int main() { class ostream; }
John Kalane
  • 1,163
  • 8
  • 17
  • 1
    [Don't use `using namespace std;`](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-a-bad-practice-in-c) – ipc Jan 03 '13 at 20:47

1 Answers1

6

There are two classes named ostream in scope: ::std::ostream and ::ostream. If you want to forward declare you have to do it in the correct namespace:

#include <iosfwd>
using namespace std;
namespace std {
    class ostream;
}
int main() { class ostream; }

Anyway, this won't work in this case because ostream is a typedef of basic_ostream, not a separate class. Just include the iosfwd header as it forward declares everything for you.

Pubby
  • 51,882
  • 13
  • 139
  • 180