今天看了一下智能指针的东西,发现更简单的思路:
就是Block和solver构造时,需要传递unique_ptr,那我们将普通指针转换成unique_ptr不就可以了么:
// 初始化g2o
//第一步:
typedef g2o::BlockSolver< g2o::BlockSolverTraits<6,3> > Block; // pose 维度为 6, landmark 维度为 3
//第二步:
Block::LinearSolverType* linearSolver = new g2o::LinearSolverCSparse<Block::PoseMatrixType>(); // 线性方程求解器
//std::unique_ptr<Block::LinearSolverType> linearSolver ( new g2o::LinearSolverCSparse<Block::PoseMatrixType>());
//第三步
Block* solver_ptr = new Block ( std::unique_ptr<Block::LinearSolverType>(linearSolver) );
//std::unique_ptr<Block> solver_ptr ( new Block ( linearSolver));
//std::unique_ptr<Block> solver_ptr ( new Block ( std::move(linearSolver))); // 矩阵块求解器
//第四步:
g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgorithmLevenberg ( std::unique_ptr<Block>(solver_ptr));
//g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgorithmLevenberg ( std::move(solver_ptr));
//第五步:
g2o::SparseOptimizer optimizer;
//第六步:
optimizer.setAlgorithm ( solver );
变化在第三步和第四步的new构造参数里,运行没毛病。
手机扫一扫
移动阅读更方便
你可能感兴趣的文章