lundi 18 juin 2018

Why won't std::bind compile when bound to a member function? [duplicate]

This question already has an answer here:

I am currently working on a program that needs to use std::bind to bind arguments to a member function, but when I attempt to do so, I get a compiler error. The following is a minimum example:

Map.h:

#pragma once

class Map {
    void buildDistances();
    void buildDistances(unsigned islandId);

};

Map.cpp:

#include "Map.h"

#include <functional>

void Map::buildDistances() {
    for(unsigned islandId=0;islandId<24;++islandId){
        auto f = std::bind(&Map::buildDistances, this, islandId);
    f();
    }
}

void Map::buildDistances(unsigned islandId) {}

Compiling with the command g++ Map.cpp -c -o map.o in the same directory as both of these files produces the following error:

Map.cpp: In member function ‘void Map::buildDistances()’:
Map.cpp:7:64: error: no matching function for call to ‘bind(<unresolved overloaded function type>, Map*, unsigned int&)’
         auto f = std::bind(&Map::buildDistances, this, islandId);
                                                                ^
In file included from Map.cpp:3:
/usr/include/c++/8.1.1/functional:808:5: note: candidate: ‘template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...)’
     bind(_Func&& __f, _BoundArgs&&... __args)
     ^~~~
/usr/include/c++/8.1.1/functional:808:5: note:   template argument deduction/substitution failed:
Map.cpp:7:64: note:   couldn't deduce template parameter ‘_Func’
         auto f = std::bind(&Map::buildDistances, this, islandId);
                                                                ^
In file included from Map.cpp:3:
/usr/include/c++/8.1.1/functional:832:5: note: candidate: ‘template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)’
     bind(_Func&& __f, _BoundArgs&&... __args)
     ^~~~
/usr/include/c++/8.1.1/functional:832:5: note:   template argument deduction/substitution failed:
Map.cpp:7:64: note:   couldn't deduce template parameter ‘_Result’
         auto f = std::bind(&Map::buildDistances, this, islandId);

Why is this happening, and how can I fix it? I attempted to find a result by putting parts of the error in a search engine, but nothing useful came up. I also tried compiling with clang, which produces the same error.

Aucun commentaire:

Enregistrer un commentaire