首页 > 解决方案 > travis 仅构建失败:使用已删除的函数 'std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&)

问题描述

该代码在本地编译没有问题(ubuntu 19.04,gcc-8.3.0),但在 travis 构建期间失败(ubuntu 18,gcc-8.3.0)。

错误

In file included from /usr/include/c++/7/memory:64:0,
                 from ./src/Game.hpp:5,
                 from ./src/main.cpp:3:
/usr/include/c++/7/bits/stl_construct.h: In instantiation of ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::unique_ptr<Cell>; _Args = {const std::unique_ptr<Cell, std::default_delete<Cell> >&}]’:
/usr/include/c++/7/bits/stl_uninitialized.h:83:18:   required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Cell>*, std::vector<std::unique_ptr<Cell> > >; _ForwardIterator = std::unique_ptr<Cell>*; bool _TrivialValueTypes = false]’
/usr/include/c++/7/bits/stl_uninitialized.h:134:15:   required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Cell>*, std::vector<std::unique_ptr<Cell> > >; _ForwardIterator = std::unique_ptr<Cell>*]’
/usr/include/c++/7/bits/stl_uninitialized.h:289:37:   required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::unique_ptr<Cell>*, std::vector<std::unique_ptr<Cell> > >; _ForwardIterator = std::unique_ptr<Cell>*; _Tp = std::unique_ptr<Cell>]’
/usr/include/c++/7/bits/stl_vector.h:331:31:   required from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::unique_ptr<Cell>; _Alloc = std::allocator<std::unique_ptr<Cell> >]’
/usr/include/c++/7/bits/stl_construct.h:75:7:   required from ‘void std::_Construct(_T1*, _Args&& ...) [with _T1 = std::vector<std::unique_ptr<Cell> >; _Args = {const std::vector<std::unique_ptr<Cell, std::default_delete<Cell> >, std::allocator<std::unique_ptr<Cell, std::default_delete<Cell> > > >&}]’
/usr/include/c++/7/bits/stl_uninitialized.h:83:18:   required from ‘static _ForwardIterator std::__uninitialized_copy<_TrivialValueTypes>::__uninit_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::vector<std::unique_ptr<Cell> >*, std::vector<std::vector<std::unique_ptr<Cell> > > >; _ForwardIterator = std::vector<std::unique_ptr<Cell> >*; bool _TrivialValueTypes = false]’
/usr/include/c++/7/bits/stl_uninitialized.h:134:15:   required from ‘_ForwardIterator std::uninitialized_copy(_InputIterator, _InputIterator, _ForwardIterator) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::vector<std::unique_ptr<Cell> >*, std::vector<std::vector<std::unique_ptr<Cell> > > >; _ForwardIterator = std::vector<std::unique_ptr<Cell> >*]’
/usr/include/c++/7/bits/stl_uninitialized.h:289:37:   required from ‘_ForwardIterator std::__uninitialized_copy_a(_InputIterator, _InputIterator, _ForwardIterator, std::allocator<_Tp>&) [with _InputIterator = __gnu_cxx::__normal_iterator<const std::vector<std::unique_ptr<Cell> >*, std::vector<std::vector<std::unique_ptr<Cell> > > >; _ForwardIterator = std::vector<std::unique_ptr<Cell> >*; _Tp = std::vector<std::unique_ptr<Cell> >]’
/usr/include/c++/7/bits/stl_vector.h:331:31:   required from ‘std::vector<_Tp, _Alloc>::vector(const std::vector<_Tp, _Alloc>&) [with _Tp = std::vector<std::unique_ptr<Cell> >; _Alloc = std::allocator<std::vector<std::unique_ptr<Cell> > >]’
./src/Systems/Physics/Grid.hpp:9:7:   required from here
/usr/include/c++/7/bits/stl_construct.h:75:7: error: use of deleted function ‘std::unique_ptr<_Tp, _Dp>::unique_ptr(const std::unique_ptr<_Tp, _Dp>&) [with _Tp = Cell; _Dp = std::default_delete<Cell>]’
     { ::new(static_cast<void*>(__p)) _T1(std::forward<_Args>(__args)...); }
       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/memory:80:0,
                 from ./src/Game.hpp:5,
                 from ./src/main.cpp:3:
/usr/include/c++/7/bits/unique_ptr.h:388:7: note: declared here
       unique_ptr(const unique_ptr&) = delete;

唯一建造的地方unique_ptr<Cell>就是这里

Grid::Grid(float gridLength, float halfWidth) : \
            gridLength(gridLength),
            cellsInRow(ceilf(gridLength / (2 * halfWidth))), 
            halfWidth(halfWidth),
            collisionDetector()
{
    this->cells.resize(this->cellsInRow);
    // cell insertion
    for (int row = 0; row < this->cells.size() ; row++)
    {
        this->cells[row].reserve(this->cellsInRow);
        for (int col = 0; col < this->cellsInRow; col++)
        {
            float x = col * 2 * halfWidth + halfWidth;
            float z = row * 2 * halfWidth + halfWidth;
            this->cells[row].push_back(std::make_unique<Cell>(glm::vec3(x, 0.f, z), halfWidth, row, col));
        }
    }
}

这段代码中的某处似乎调用了 unique_ptr 的复制构造函数,但我不知道在哪里。

编辑 :

Grid.hpp

#pragma once

#include <memory>
#include <vector>
#include "Cell.hpp"
#include "Collider.hpp"
#include "CollisionDetector.hpp"

class Grid
{
    public:

        /**
        Divides the 3d space into a 2d grid
         */
        Grid(float gridLength, float halfWidth);
        /** 
        Insert a Collider collider
         */
        void Insert(std::shared_ptr<Collider>   object);

        /** 
        Deletes an object from the grid.
         */
        void Remove(std::shared_ptr<Collider>   object);

        /**
        Performs a collision check on all the cells and generates contact data.
         */
        std::vector<std::shared_ptr<Collision>> CheckCollisions();

        std::vector<std::shared_ptr<Collision>> CheckCells(int rowA, int colA, int rowB, int colB);

        // ===============
        // Utility methods
        // ===============

        /** 
        Returns a vector of pairs denoting the cells to be checked for collision detection
        */
        std::vector<std::pair<int, int>> GetEligibleCells(int cellRow, int cellCol);

        /** 
        GetInsertRow returns the row of the cell that the object needs to get inserted into
         */
        int  GetInsertRow(glm::vec3 point);

        /** 
        GetInsertCol returns the column of the cell where the object should be inserted.
         */
        int  GetInsertCol(glm::vec3 point);

        const std::vector< std::vector< std::unique_ptr<Cell>>>& GetCells();

    private:

        int     cellsInRow;
        float   halfWidth;
        float   gridLength;
        CollisionDetector collisionDetector;
        std::vector< std::vector< std::unique_ptr<Cell> > > cells;
};

网格仅由PhysicsSystem

PhysicsSystem::PhysicsSystem(float gridLength, float cellHalfWidth) : grid(gridLength, cellHalfWidth)
{
    this->primaryBitset = ComponentType::Physics | ComponentType::Transform;
}

而物理系统只在这里初始化

Game::Game(int width, int height) :
    width(width), height(height) , 
    physicsSystem(70.f, 5.f)
{
   // stuff
}

标签: c++11unique-ptr

解决方案


推荐阅读