迁移C++代码以使用标准标头.现在得到:引用'ostream'模棱两可

sai*_*eak 0 c++ std ostream

我一直在迁移一些代码来改变头文件的声明,因为它们不包含在我的Ubuntu环境中.我终于改变了所有文件,但是收到了以下错误:

Item.h:33: error: reference to ‘ostream’ is ambiguous
Item.h:16: error: candidates are: struct ostream
/usr/include/c++/4.4/iosfwd:130: error:                 typedef struct  
std::basic_ostream<char, std::char_traits<char> > std::ostream 
Item.h:33: error: ISO C++ forbids declaration of ‘ostream’ with no type
Item.h:33: error: ‘ostream’ is neither function nor member function; cannot be declared friend
Run Code Online (Sandbox Code Playgroud)

代码如下:

class Item
{
public:
    Item( //const Wrd* hd,
     const Term * _term, int _start, int _finish );
    ~Item();
    int         operator== (const Item& item) const;
    friend ostream& operator<< ( ostream& os, const Item& item ); // <-- error
Run Code Online (Sandbox Code Playgroud)

有谁知道我会如何纠正这个?

tem*_*def 5

看起来在Item.h中你有一行如下所示:

struct ostream;
Run Code Online (Sandbox Code Playgroud)

你得到的问题ostream是不是struct; 它是一个typedeffor basic_ostream<char>,所以你的自定义定义ostreamostream前向声明的标准定义冲突<iosfwd>.因此,当你写作

friend ostream& operator<< ( ostream& os, const Item& item );
Run Code Online (Sandbox Code Playgroud)

编译器无法判断是否ostream引用了您struct ostreamtypedef标准头文件导出的更复杂的内容.

要解决此问题,请找到您尝试转发声明ostream并删除它的位置.相反,请考虑使用头文件<iosfwd>导入前向引用ostream.

更一般地说,您不应该尝试在标准库中向前声明任何内容.只是#include适当的标题.