在VC6中我最近使用
#include <iosteam>
using namespace std;之后重载操作符<<,定义为类的友员,如:
class A
{
public:
   friend ostream& operator<<(ostream& out, const A& x);
private:
   int m_i;
};ostream operator<<(ostream& out, const A& x)
{
   out << x.m_i << " ";//error
   return out;
}//error处错误,说我不能够访问私有成员。但在unix下就好用。哪位大侠能够深入分析一下这个问题。
当然,如果不使用STL而用,#include <iostream.h>就不会error。

解决方案 »

  1.   

    这是由于using namespace引起的,编译器以为要重新定义std中的operator<<.
    如果这样就行了:
    #include <iostream>
    //using namespace std;//using namespace std;
    class A
    {
    public: friend std::ostream& operator <<(std::ostream& out,A &x);
       private:
       int m_i;
    };std::ostream& operator <<(std::ostream& out,  A &x)
    {
      out << x.m_i << " ";//error
       //x.m_i =0;
    return out;
    }
    using namespace std;当然最好这样
    #include <iostream>
    //using namespace std;//using namespace std;
    class A
    {
    public: friend std::ostream& operator <<(std::ostream& out,A &x);
             { out << x.m_i << " ";//error
       return out;
              }
    private:
       int m_i;
    };/*std::ostream& operator <<(std::ostream& out,  A &x)
    {
      out << x.m_i << " ";//error
       return out;
    }
    */
    using namespace std;
      

  2.   

    后一种写错了应是:
    class A
    {
    public: friend std::ostream& operator <<(std::ostream& out,A &x);
       private:
       int m_i;
    };std::ostream& operator <<(std::ostream& out,  A &x)
    {
      out << x.m_i << " ";//error
       //x.m_i =0;
    return out;
    }
    using namespace std;当然最好这样
    #include <iostream>
    using namespace std;class A
    {
    public: friend std::ostream& operator <<(std::ostream& out,A &x);
             { out << x.m_i << " ";//error
       return out;
              }
    private:
       int m_i;
    };
    不好意思
      

  3.   

    谢谢zwert(小张),我想最后使用你所说的后一种方法,因为如果我使用template,那么使用第一种方法,或许会出问题。