2016-03-23 3 views
1

私はshared_ptrのベクトルを持っています。find_ifとboost :: bindをshared_pointersのセットで使用する

私の質問は、代わりのものを除いて、thisに非常によく似ている「& MyClassの:: ReferenceFnは、」私は「&要素::のFn」を呼び出すしたいと思います。

は、ここでは、コードの類似した作品です。

no matching function for call to ‘bind(<unresolved overloaded function type>, const boost::reference_wrapper<B>, boost::arg<1>&)’ 

:ここ

typedef boost::shared_ptr<Vertex> vertex_ptr; 
std::set<vertex_ptr> vertices; 

void B::convert() 
{ 
... 
if(std::find_if(boost::make_indirect_iterator(vertices.begin()), 
       boost::make_indirect_iterator(vertices.end()), boost::bind(&Vertex::id, boost::ref(*this), _1) == (*it)->id()) == vertices.end()) 
} 

エラーです私はC++ 03を使用することに限定されています。

+0

エラーはどうなりますか? –

+0

@PiotrSkotnicki、エラーは、このクラスBのポインタとしてこのキーワードを取ることです。 –

+2

boost :: bind(&Vertex :: id、_1)==(* it) - > id()) '(int型は' 'int型' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '' '')戻り値の型は 'id') –

答えて

2

コレクションに保存されている各オブジェクトのメンバ関数を呼び出すには、boost::bindの最初のバウンドの引数としてプレースホルダを使用する必要があります。

boost::bind(&Vertex::id, _1) == (*it)->id()) 
//      ~^~ 

この方法で、各引数aは、にバインドされますメンバ関数ポインタであり、(a.*&Vertex::id)()と呼ばれます。

ただし、エラーメッセージunresolved overloaded function typeと表示されていることから、クラスVertexにはメンバー関数idの複数のオーバーロードがある可能性があることがわかります。このように、コンパイラは、引数として渡すべきものをboost::bindとして伝えることはできません。この問題を解決するには、メンバ関数ポインタへの明示的なキャストを使用します(コロンの後にアスタリスクは、それがメンバへのポインタです示す):、Vertexは複数のオーバーロードがありケースクラスで

boost::bind(static_cast<int(Vertex::*)()const>(&Vertex::id), _1) == (*it)->id()) 
//      ~~~~~~~~~~~~~~~~~~~~^ 

は言う:

int id() const { return 0; } 
void id(int i) { } 

最初のバインディングをバインディングに使用します。

関連する問題