0

私の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

コードは次のとおりです。

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

これを修正する方法を知っている人はいますか?

4

3 に答える 3

5

Item.h には、次のような行があるようです。

struct ostream;

あなたが得ている問題は、それが;でostreamはないということです。forでstructあるため、 のカスタム定義は、で前方宣言されている の標準定義と競合しています。したがって、あなたが書くときtypedefbasic_ostream<char>ostreamostream<iosfwd>

friend ostream& operator<< ( ostream& os, const Item& item );

コンパイラは、標準ヘッダー ファイルによってエクスポートされた、またはより複雑なファイルostreamを参照しているかどうかを判断できません。struct ostreamtypedef

これを修正するには、前方宣言を試みた場所を見つけてostream削除します。代わりに、ヘッダー ファイル<iosfwd>を使用して への前方参照をインポートすることを検討してくださいostream

より一般的には、標準ライブラリで何かを前方宣言しようとすべきではありません。それ#includeに適したヘッダーです。

于 2011-03-20T19:38:48.653 に答える
1

コンパイラは、何が起こっているかを正確に伝えます。

という名前のテンプレート特殊化 ( basic_ostream<char...>)の typedef がありostream(これは明らかに標準ヘッダーに由来しstruct ostreamます)、コード内の別の場所に定義されています (これを見つけて名前を変更/ラムーブする必要があります)。したがって、あいまいさ

于 2011-03-20T19:37:40.813 に答える
0

試しましたfriend std::ostream& operator<< ...か?ヘッダーの残りの部分を見ずに答えるのは難しいです。

于 2011-03-20T19:35:55.857 に答える