朋友ostream无法访问私人会员

时间:2016-06-30 08:05:12

标签: c++ operator-overloading iostream

我已经看到过这样的其他问题,但我没有得到解决方案。  这是代码:

cout_overload.h:

#ifndef COUT_OVERLOAD_H_
#define COUT_OVERLOAD_H_

          #include <iostream>

          class A
          {
                  private:
                          int data;
                  public:
                          A(int d);
                          friend std::ostream & operator << 
                            (std::ostream os, const A &t );
          };

 #endif

cout_overload_r.cpp:

   #include <iostream>                                                                       
   #include "cout_overload.h"

   A::A(int d)
   {
           data = d;
   }

   std::ostream &operator << (std::ostream &os, const A&t)
   {
          os << " t = " << t.data ;
          return os;
   }       

main.cpp:

#include <iostream>                                                                        #include "cout_overload.h"

 int main(void)
  {
          A ra(1);
          using std::cout;

  //      cout<<ra;

         return 0;
 }

编译时的错误: enter image description here

1 个答案:

答案 0 :(得分:3)

您需要修改request(target, function (err, resp, body) { $ = cheerio.load(body); links = $('a'); $(links).each(function (i, link) { if ($(link).text().match('worker')) { a = $(link).attr('href').toString(); // (*) } }); }); 功能并在

中使用friend
ostream&

并替换上面的行,

friend std::ostream & operator << (std::ostream os, const A &t );

因为friend std::ostream & operator << (std::ostream &os, const A &t ); 是输出流,ostream将通过引用传递(将流传递给函数的唯一方法)..