Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mem_fun_ref: unresolved overloaded function type

Tags:

c++

std

The following code won't compile because of "error: no matching function for call to ‘mem_fun_ref()’" (gcc version 4.4.6).

#include <vector>
#include <string>
#include <string.h>
#include <algorithm>
#include <iostream>

using namespace std;

class toto
{
  char v[10];

public:
  toto(char* t) { memcpy(v, t, 9); }
  bool test(const char* var) const { return !strncmp(var, v, 9); }
  bool test(const string& var) const { return test(var.c_str()); }
};

int main()
{
  vector<toto> t;
  t.push_back("1");
  t.push_back("2");

  string name("2");
  vector<toto>::iterator it = remove_if(t.begin(), t.end(),
       bind2nd(mem_fun_ref(&toto::test), name)); // <= error
  t.erase(it, t.end());
  return 0;
}

I found a workaround: creating a

bool testZ(const string& var) const { return testZ(var); }

But I can't seem to find the correct template parameters, if that's even possible, to give to mem_fun_ref (or bind2nd?) to make it compile without my workaround.

Is there anyway to achieve this without my workaround, or is the workaround the "preferred" method?

like image 949
BlakBat Avatar asked Mar 14 '26 13:03

BlakBat


1 Answers

You should be able to cast it according to C++ overloaded method pointer:

bind2nd(mem_fun_ref((bool (toto::*)(const string&) const) &toto::test), name));

Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!