none
error C2679: binary '<<' ...

    Question

  • consider this piece of code:
    #include "stdafx.h"
    #include <list>
    #include <iostream>
    struct my {
        int i;
        int j;
    };

    int _tmain(int argc, _TCHAR* argv[])
    {
        my m;
        std::list< my > m1;
        std::list< my >::iterator it;
        m.i = 1;
        m.j = 2;
        m1.push_back( m );
        m.i = 8;
        m.j = 6;
        m1.push_back( m );
        m1.sort;
        for ( it = m1.begin(); it != m1.end(); it++ )
            std::cout << it;   //error
        return 0;
    }

    why I get this error:

    d:\mahmood\Visual Studio Projects\test21\test21\test21.cpp(25) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::list<_Ty>::iterator' (or there is no acceptable conversion)
            with
            [
                _Ty=my
            ]
    Tuesday, April 08, 2008 10:13 AM

Answers

  • As the error says....there is no operator '<<' available that is able to print out an iterator of type 'std::list<my>'. You would either need to provide such an operator or print them out separately:

    std::cout << it->i << " " << it->j;
    Tuesday, April 08, 2008 10:29 AM
  •  

    Code Snippet

    #include <iostream>
    #include <algorithm>
    #include <list>


    struct my
    {
      int i;
      int j;


      bool operator <(const my& other) const
      {
        return i < other.i;
      }
    };


    int main(int argc, char* argv[])
    {
      my m;
      std::list<my> m1;
      std::list<my>::iterator it;
      m.i = 8;
      m.j = 6;
      m1.push_back(m);
      m.i = 1;
      m.j = 2;
      m1.push_back(m);
      m1.sort();
      for (it = m1.begin(); it != m1.end(); ++it)
        std::cout << it->i << ' ' << it->j << std::endl;
      return 0;
    }

     

     

     

    Tuesday, April 08, 2008 4:20 PM

All replies

  • As the error says....there is no operator '<<' available that is able to print out an iterator of type 'std::list<my>'. You would either need to provide such an operator or print them out separately:

    std::cout << it->i << " " << it->j;
    Tuesday, April 08, 2008 10:29 AM
  • Thanks. You are right.

    Also I have another question. If I define a list of such struct, then by performing sort function, what will be the result.
    I tested it and found no change. for example, for (3, 4) and (2, 6) the output after sort is:
    3 4
    2 6

    for more than one function, what does sort function do? based on which field?
    Tuesday, April 08, 2008 10:38 AM
  • The sort algorithm depends upon operator <, if you have not defined one for your struct, it will not know what to do. Same as the ostream problem you had (see previous reply). These problems are discussed in great detail on various web pages, try Googling.
    Tuesday, April 08, 2008 11:54 AM
  • I have searched with some keyword like "stl sort for struct" but I did not find any good material.

    So... if I want to force sort function to sort field i in above struct, then what should I do? I can not figure the relation of this with Andreas idea!!!??
    Tuesday, April 08, 2008 3:50 PM
  • You would find it really worthwhile to read "Effective STL" by Scott Myers

    Tuesday, April 08, 2008 4:13 PM
  •  

    Code Snippet

    #include <iostream>
    #include <algorithm>
    #include <list>


    struct my
    {
      int i;
      int j;


      bool operator <(const my& other) const
      {
        return i < other.i;
      }
    };


    int main(int argc, char* argv[])
    {
      my m;
      std::list<my> m1;
      std::list<my>::iterator it;
      m.i = 8;
      m.j = 6;
      m1.push_back(m);
      m.i = 1;
      m.j = 2;
      m1.push_back(m);
      m1.sort();
      for (it = m1.begin(); it != m1.end(); ++it)
        std::cout << it->i << ' ' << it->j << std::endl;
      return 0;
    }

     

     

     

    Tuesday, April 08, 2008 4:20 PM
  • Thanks for your help
    Wednesday, April 09, 2008 7:05 AM