0
我有一个类Foo
含有Bar
s的vector
:C++矢量的push_back与函数指针
class Foo
{
public:
void create();
void callback();
std::vector<Bar> mBars;
}
我Bar
类包含此构造:
class Bar
{
Bar(const int x, const int y, std::function<void()> &callback);
}
我Foo
类有一个create()
方法,将Bar
s添加到mBars
向量中:
void Foo::create()
{
mBars.push_back({ 1224, 26, callback }); //ERROR!
}
如何设置函数指针,使用std::function
?也没有创建一个单独的对象和push_back
成矢量?像上面的线,在那里我得到的错误:
E0304 no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=CV::Button, _Alloc=std::allocator<Bar>]" matches the argument list
你没有拿这个函数的指针。另外对于'std :: function',你必须使用'std :: bind'。 – tambre
另外,您可能想查看['vector :: emplace_back()'](http://en.cppreference.com/w/cpp/container/vector/emplace_back)。 –