Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        4年前关闭。
                                                                                            
                
        
我有一个不可变的标头:

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)


请注意,无法触摸标题,我该怎么办?

最佳答案

如果那是整个标题,那么它就坏了。它缺少#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>(不能保证前者包括后者),而我仅从提供的有限信息中就可以猜到这一切。

关于c++ - 重载ostream <<运算符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33467562/

10-13 03:46