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; }
4

1 回答 1

6

在范围内命名了两个类ostream::std::ostream::ostream。如果要转发声明,则必须在正确的命名空间中进行:

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

无论如何,这在这种情况下不起作用,因为ostream它是 的 typedef basic_ostream,而不是单独的类。只需包含iosfwd标题,因为它向前声明了所有内容。

于 2013-01-03T20:44:51.357 回答