1

Here is the code

#include <string>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

class test {
    struct con {
        string s1;
        string s2;
    };
public:
    void somefunc();

private:
    bool pred(con c1, con c2);
    vector<con> vec;
};

void test::somefunc()
{
    vector<con> v;
    vector<con>::iterator it = find_first_of(vec.begin(), vec.end(), v.begin(), v.end(), pred);
}

bool test::pred(con c1, con c2)
{
    return 0;
}

it gives error

est.cpp(24) : error C2664: 'struct test::con *__cdecl std::find_first_of(struct test::con *,struct test::con *,struct test::con *,struct test::con *,bool (__thiscall *)(struct test::con,str uct test::con))' : cannot convert parameter 5 from 'bool (struct test::con,struct test::con)' to 'bool (__thiscall *)(struct test::con,struct test::con)' None of the functions with this name in scope match the target type

I don't understand what is (__thiscall*) and how to convert my predicate function to it.

user2282875
  • 75
  • 1
  • 2
  • 6
  • 2
    Member function pointers are not the same as function pointers. You can't reasonably convert them. – chris Jun 11 '13 at 16:01

3 Answers3

2

Your predicate cannot be a non-static member function, since this takes an implicit first parameter, giving a total of three parameters. You need either a static member function, or a non-member:

// non-member function
bool pred(const con& c1, const con& c2)
{
    return false;
}

void test::somefunc()
{
  vector<con> v;
  vector<con>::iterator it = find_first_of(vec.begin(), vec.end(), 
                                           v.begin(), v.end(), 
                                           pred);
}

Alternatively, use std::bind to bind this as the first parameter:

using namespace std::placeholders;
vector<con>::iterator it = find_first_of(vec.begin(), vec.end(), 
                                         v.begin(), v.end(),
                                         std::bind(&test::pred, this, _1, _2));

The call to std::bind produces a callable entity with with two con parameters. It stores a copy of the this pointer internally (but this isn't used in your pred function).

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
1

__thiscall is a calling convention which is used for non-static member function only by MSVC++.

In your code, pred is a non-static member function, which cannot be used as binary-predicate without some workaround such as std::bind. I would rather suggest you to make this function static. Or even better use lamda (C++11 only).

Community
  • 1
  • 1
Nawaz
  • 353,942
  • 115
  • 666
  • 851
0

Your predicate is a non-static member function. Either make it static (or simply a non-member function), or use std::bind() (or the equivalent Boost.Bind if you can't afford C++11):

#include <functional>

// ...

vector<con>::iterator it = find_first_of(
    vec.begin(), vec.end(), v.begin(), v.end(),
    std::bind(&test::pred, this, std::placeholders::_1, std::placeholders::_2));

Here is a live example.

If you choose to keep your function as a member function and use std::bind(), consider qualifying your function pred() as const, since it is likely not supposed to alter the state of the object it is invoked on.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451