This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

could list's object be used with (for_each)?


Dear advanced c++ programers:

  in c++ cook book's example 4-8, it use list's object with algorithm
for_each, that can not get compiled in my
g++ compiler.   Is that normal(author's fault) or my g++ side's
responsibility?
/* I get some google search, I found most of them use vector
 * but in its definition , it did not forbid using list, at least
under my search capability reach so far
 */
You can get code from here.
http://examples.oreilly.com/9780596007614/

----------
eric@eric-laptop:~/cppcookbook/download$ g++ 4-8.cpp
4-8.cpp: In function ‘int main()’:
4-8.cpp:25:44: error: no matching function for call to
‘for_each(std::list<std::basic_string<char> >::iterator,
std::list<std::basic_string<char> >::iterator, <unresolved overloaded
function type>)’
eric@eric-laptop:~/cppcookbook/download$ cat 4-8.cpp
#include <list>
#include <algorithm>
#include <iostream>

using namespace std;

void write(const string& s) {
   cout << s << '\n';
}

int main( ) {
   list<string> lst;

   string s = "knife";
   lst.push_front(s);

   s = "fork";
   lst.push_back(s);

   s = "spoon";
   lst.push_back(s);

   // A list doesn't have random access, so
   // use for_each( ) instead
   for_each(lst.begin( ), lst.end( ), write);
}
eric@eric-laptop:~/cppcookbook/download$
----------------------------------------------------------------------


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]