C ++:没有匹配的功能

时间:2017-09-21 16:25:55

标签: c++ bind functor

我正在尝试复制我遇到的问题,但现在代码失败了。

到目前为止,

代码是:

namespace
{
    // this cannot change either - needs to be static
   static bool imp( const int  a , const int b )
   {
      return a != b ;
   }
}

template < typename KEY , 
           typename VALUE, 
           typename CALLBACK = VALUE(*)( const KEY & ) > class ComplexObject
{
     public :
     ComplexObject( CALLBACK ){} ;
     ///......
     // cannot change that so cannot provide default constructor!!!
     //.... more functions that utilite KEY.VALUE - imagine this is a cache
};

using namespace std::placeholders;  // for _1, _2, _3...

typedef std::function<bool(*)( const int)> myFunctor ; 

class TypeA
{
public:
    TypeA(const int id) : n_id(id) ,  n_co( std::bind( &imp , n_id , _1 ) ) {}
    bool check( const int a, const int id ) ;
private :
    int n_id;
    ComplexObject < int, bool, myFunctor > n_co ;
protected :
     TypeA() : n_id(0) , n_co( std::bind( &imp , n_id , _1 )) { }
};

here

错误是:

g++ -std=c++14 -O2 -Wall -pedantic -pthread main.cpp && ./a.out
main.cpp: In constructor 'TypeA::TypeA(int)':
main.cpp:32:75: error: no matching function for call to 'ComplexObject<int, bool, std::function<bool (*)(int)> >::ComplexObject(std::_Bind_helper<false, bool (*)(int, int), int&, const std::_Placeholder<1>&>::type)'
     TypeA(const int id) : n_id(id) ,  n_co( std::bind( &imp , n_id , _1 ) ) {}

1 个答案:

答案 0 :(得分:1)

std::function<>模板参数是函数类型,而不是指向函数类型的指针

以下更改修复了它:

if var1[-1] != '/':
   print("whatever")

只有在C ++ 17中,您才能使用std::function<bool(*)(int)>