title: Cannot bind operator << in cpp
body: <p>Guru,</p>
  
  <p>Given two classes defined as follow (attributes, methods, and implementation omitted) :</p>
  
  <pre><code>struct A { friend std::ostream&amp; operator &lt;&lt; (std::ostream&amp; o, const A&amp; c); };
  struct B { friend std::ostream&amp; operator &lt;&lt; (std::ostream&amp; o, const B&amp; c); };
  </code></pre>
  
  <p>I used the classes as follow:</p>
  
  <pre><code>ln 1: A *arrayA = new A[10];
  ln 2: B *arrayB = new B[10];
  ln 3: /* some codes to initialize arrayA and arrayB */
  ln 4: for (int i = 0; i &lt; 10; i++) { std::cout &lt;&lt; arrayA[i]; } // this work 
  ln 5: for (int j = 0; j &lt; 10; j++) { std::cout &lt;&lt; arrayB[j]; } // this complain
  </code></pre>
  
  <p>My compiler complains for class B as </p>
  
  <pre><code>error: cannot bind 'std::ostream {aka std::basic_ostream&lt;char&gt;}' lvalue 
  to 'std::basic_ostream&lt;char&gt;&amp;&amp;'
  
  ../lib/gcc/mingw32/4.6.1/include/c++/ostream:581:5 error initializing argument 1 
  of 'std::basic_ostream&lt;_CharT, _Traits&gt;&amp; 
  std::operator&lt;&lt;(std::basic_ostream&lt;_CharT, _Traits&gt;&amp;&amp;, const _Tp&amp;) 
   [with _CharT = char, _Traits = std::char_traits&lt;char&gt;, _Tp = ClsB]
  </code></pre>
  
  <p>I don't know what's wrong with line 5. Commenting out line 5 of the main program give my good compilation, meaning my definition of the operator&lt;&lt; for class B is syntactically correct. Please give any directions and thanks.</p>
  
  <p>Yam Hon</p>
  
  <ul>
  <li>Platform: win 7</li>
  <li>MinGW32: version 2011-11-08</li>
  <li>GNU Make 3.82</li>
  <li>G++ version 4.6.1</li>
  </ul>
  
  <p>[Edit 1]
  My program actually has more than two classes, and all my class have operator&lt;&lt; overloaded for debugging. I used the same signature (with appropriate 2nd argument) for all classes. Only class B give this error.</p>
  
  <p>[Edit 2]
  full version of my classes:</p>
  
  <pre><code>struct CPeople { // this is class B
  int age;
  int ageGroup;
  
  int zipcode;
  int communityID;
  int areaID;
  int familyID;
  int contactID;
  int contactType; /* P, D, E, M, H, W */
  int state;
  int vaccinated; /* 0 = unvac, 1 = vaccinated */
  
  friend std::ostream&amp; operator&lt;&lt; (std::ostream&amp; o, const CPeople&amp; c)
  {
      o &lt;&lt; "CPeople (" &lt;&lt; static_cast&lt;void const *&gt;(&amp;c) &lt;&lt; "): "
        &lt;&lt; "\tAge Group: "   &lt;&lt; c.ageGroup
        &lt;&lt; "\tZip Code: "    &lt;&lt; c.zipcode
        &lt;&lt; "\tCommunityID: " &lt;&lt; c.communityID
        &lt;&lt; "\tArea ID: "     &lt;&lt; c.areaID
        &lt;&lt; "\tFamily ID: "   &lt;&lt; c.familyID
        &lt;&lt; "\tSchool Type: " &lt;&lt; c.contactType
        &lt;&lt; "\tContact ID: "  &lt;&lt; c.contactID
        &lt;&lt; "\tState: "       &lt;&lt; c.state
        &lt;&lt; "\tVaccination: " &lt;&lt; c.vaccinated;
      return (o &lt;&lt; std::endl);
  }
  };
  
  struct CWorkGroup : public CContact { // this is class A
  /* to which community this member belongs */
  std::vector&lt;long&gt; member_com;
  CStatistics statistics;
  
  friend std::ostream&amp; operator&lt;&lt; (std::ostream&amp; o, const CWorkGroup&amp; c)
  {
      o &lt;&lt; "CWorkGroup (" &lt;&lt; static_cast&lt;void const *&gt;(&amp;c) &lt;&lt; "): ";
      o &lt;&lt; "avflag = " &lt;&lt; c.avflag &lt;&lt; "; member: " &lt;&lt; c.size();
      for (int i = 0; i &lt; c.size(); i++)
      {
          o &lt;&lt; "; (" &lt;&lt; i &lt;&lt; " = " &lt;&lt; c.member[i] &lt;&lt; ")";
      }
      o &lt;&lt; std::endl;
      return (o &lt;&lt; c.statistics);
  }
  };
  </code></pre>
  
  <p>Usage A:</p>
  
  <pre><code>for (int i = 0; i &lt; cntWG; i++) { std::clog &lt;&lt; WG[i]; } std::clog &lt;&lt; std::endl;
  </code></pre>
  
  <p>Usage B (this is error):</p>
  
  <pre><code>CPeople *people_total = new CPeople[cntTotalPop];
  for (pIdx = 0; pIdx &lt; cntTotalPop; pIdx++)
  {
      std::cout &lt;&lt; people_total[pIdx];
  }
  </code></pre>