2015-11-01 130 views
0

我有一個不變的重載ostream的<<操作

typedef class Foo 
{ 
    public: 
     friend ostream& operator<<(ostream&, Foo&); 
}*pFoo, **ppFoo; 

我試圖實現運營商,像這樣:

#include <iostream> 
using namespace std; 
#include "Foo.h" 

ostream& operator<<(ostream& a, Foo& b){ 
    a << endl; 
    return a; 
} 

這是拋出這些錯誤:

Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int (foo.h) 

Error 5 error C2805: binary 'operator <<' has too few parameters (foo.h) 
Error 2 error C2433: 'ostream' : 'friend' not permitted on data declarations (foo.h) 
Error 4 error C2061: syntax error : identifier 'ostream' (foo.h) 

請記住,標題不能被觸及,我該怎麼辦?

+0

好像你錯過了一些'#include'語句。另外你的_「實現」_看起來很沒用。 –

回答

2

如果這是整個標題,那麼它被打破。它缺少#include <ostream>std::

既然你不能改變它,你必須:

  • 抱怨恨恨
  • 包括列入前<ostream>using namespace std(你幾乎已經這樣做)

至於C++ 11,其實包括<iostream>其實已經夠了,as it happens, with my compiler I cannot reproduce your problem with C++03 either。但在C++ 03中,它是可能的,您需要單獨#include <ostream>(前者不保證包含後者),而這些都是我從所提供的有限信息中可以猜到的。

+0

與#include 有什麼不同? –

+0

@πάνταῥεῖ:極其如此,在C++ 11之前。 –

+0

都不起作用。既然你不能重現這個問題,我開始懷疑這是否是一個環境問題。 – TheFaster