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


E

eric

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/

----------
[email protected]:~/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>)’
[email protected]:~/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);
}
[email protected]:~/cppcookbook/download$
----------------------------------------------------------------------
 
Ad

Advertisements

H

Howard Hinnant

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/

----------
[email protected]:~/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>)’
[email protected]:~/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);}

[email protected]:~/cppcookbook/download$
----------------------------------------------------------------------

The problem is with the function "write". Your headers evidentially
include a non-standard system function named "write", and this
overloads with your "write". The compiler doesn't know which one to
choose. You can:

1. Pick another name for your "write" function, or

2. Cast it to the correct function pointer type:


for_each(lst.begin( ), lst.end( ), (void(*)(const string&))write);

Howard
 
Ad

Advertisements

E

eric

The problem is with the function "write".  Your headers evidentially
include a non-standard system function named "write", and this
overloads with your "write".  The compiler doesn't know which one to
choose.  You can:

1.  Pick another name for your "write" function, or

2.  Cast it to the correct function pointer type:

   for_each(lst.begin( ), lst.end( ), (void(*)(const string&))write);

Howard

Thanks your solution
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top