为什么将值为shared_ptrs的向量传递给lambda无法编译?

时间:2015-02-28 05:24:45

标签: c++ c++11 lambda

具体来说,这是一些代码:

#include <memory>
#include <vector>

class Obj
{
public:
    Obj(int number) : m_member(number) {}

    int m_member;
};

int main(int argc, char *argv[])
{
    std::vector<std::shared_ptr<Obj>> objPointers;

    std::function<bool(int)> eventFunction = [=](int number)
    {
        auto objP = std::make_shared<Obj>(number);
        objPointers.push_back(objP);
        std::cout << number << " " << objP->m_member << std::endl;
        return true;
    };

    eventFunction(20);

    exit(EXIT_SUCCESS);
}

无法使用此消息进行编译:{{1​​}}

我不明白为什么它认为它是error: passing 'const std::vector<std::shared_ptr<Obj> >' as 'this' argument of 'void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::shared_ptr<Obj>; _Alloc = std::allocator<std::shared_ptr<Obj> >; std::vector<_Tp, _Alloc>::value_type = std::shared_ptr<Obj>]' discards qualifiers [-fpermissive]|向量?

0 个答案:

没有答案