T
tmak
I am having trouble using mem_fun_ref on an overloaded member
function. Here's my code (using gcc 4.x):
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
int main()
{
vector<string> vs;
vs.push_back(string("One"));
vs.push_back(string("Two"));
for_each(vs.begin(),vs.end(), bind2nd (mem_fun_ref
( &string:ush_back), 's' ) );
cout << vs[0] << " and " << vs[1] << endl; //prints Ones and Twos as
expeced
//ERROR: Following line does not compile
for_each(vs.begin(),vs.end(), bind2nd(mem_fun_ref
(&string::append),"ome"));
cout << vs[0] << " and " << vs[1] << endl ; //should output Onesome
and Twosome
//This works, but not how I want to do it
for (vector<string>::iterator it=vs.begin(); it!= vs.end(); it++)
it->append("ome");
cout << vs[0] << " and " << vs[1] << endl ; //outputs Onesome and
Twosome
}
gcc (4.0.1 on Max OS X) gives this :
mem_fun_fun.cpp: In function ‘int main()’:
2 mem_fun_fun.cpp|20| error: no matching function for call to
‘mem_fun_ref(<unknown type>)’
I am guessing the compiler has no idea which of the overloaded
string::append functions to use. Any ideas on solving this.
function. Here's my code (using gcc 4.x):
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
int main()
{
vector<string> vs;
vs.push_back(string("One"));
vs.push_back(string("Two"));
for_each(vs.begin(),vs.end(), bind2nd (mem_fun_ref
( &string:ush_back), 's' ) );
cout << vs[0] << " and " << vs[1] << endl; //prints Ones and Twos as
expeced
//ERROR: Following line does not compile
for_each(vs.begin(),vs.end(), bind2nd(mem_fun_ref
(&string::append),"ome"));
cout << vs[0] << " and " << vs[1] << endl ; //should output Onesome
and Twosome
//This works, but not how I want to do it
for (vector<string>::iterator it=vs.begin(); it!= vs.end(); it++)
it->append("ome");
cout << vs[0] << " and " << vs[1] << endl ; //outputs Onesome and
Twosome
}
gcc (4.0.1 on Max OS X) gives this :
mem_fun_fun.cpp: In function ‘int main()’:
2 mem_fun_fun.cpp|20| error: no matching function for call to
‘mem_fun_ref(<unknown type>)’
I am guessing the compiler has no idea which of the overloaded
string::append functions to use. Any ideas on solving this.