制作一对共享指针并推入一个向量

Making a pair of shared pointer and pushing in a vector

我正在尝试制作一对共享指针并将其推入列表并出现错误。 错误在 list_of_objects_combination.push_back()

我的代码有什么问题?

no matching function for call to ‘Objects::Objects(__gnu_cxx::__normal_iterator*, std::vector > >&)’ { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }

void MyClass::getCombination(std::vector<std::shared_ptr< Objects>> m_list_objects,
                                 std::vector<std::pair<std::shared_ptr<Objects>, std::shared_ptr<Objects>>> &list_of_objects_combination) {

    std::vector<std::shared_ptr< Objects>>::const_iterator objectIterator = m_list_objects.begin();
    std::vector<std::shared_ptr<Objects>>::const_iterator objectIteratorNext;

    for (; objectIterator < m_list_objects.end(); objectIterator++) {
        for (objectIteratorNext = objectIterator + 1; objectIteratorNext < m_list_objects.end();
             objectIteratorNext++) {

            list_of_objects_combination.push_back(std::make_pair(std::make_shared<Objects>(objectIterator),
                                                                 std::make_shared<Objects>(objectIteratorNext)));
        }
    }
}

您正在尝试从向量迭代器中生成一对:std::make_shared<Objects>(objectIterator),而不是 Objects/shared_ptrs。尝试 std::make_pair(*objectIterator, *objectIteratorNext)。这样你就可以避免代码中的另一个错误,因为你试图对现有对象创建新的 shared_ptr,而不是复制现有对象。目前,即使 std::make_shared<Objects>(objectIterator)std::make_shared<Objects>(objectIterator->get()) 也会创建具有单独引用计数的新 shared_ptrs,这将导致双 deleting/UB.