boost::shared_ptr::shared_ptr(const boost::shared_ptr&)' 被隐式声明为已删除

boost::shared_ptr::shared_ptr(const boost::shared_ptramp;)#39; is implicitly declared as deleted(boost::shared_ptr::shared_ptr(const boost::shared_ptramp;) 被隐式声明为已删除)

本文介绍了boost::shared_ptr::shared_ptr(const boost::shared_ptr&)' 被隐式声明为已删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/make_shared.hpp>
using namespace std;

struct Node
{
    Node(int data, boost::shared_ptr<int> next = boost::make_shared<int>())
      : m_data(data), m_next(next) {}

    int m_data;
    boost::shared_ptr<int> m_next;    
};

错误:http://www.compileonline.com/compile_cpp11_online.php- 在线编译和执行 C++11(GNU GCC 版本 4.7.2)

Error: http://www.compileonline.com/compile_cpp11_online.php - Compile and Execute C++11 Online (GNU GCC version 4.7.2)

Compiling the source code....
$g++ -std=c++11 main.cpp -o demo -lm -pthread -lgmpxx -lgmp -lreadline 2>&1
main.cpp: In constructor 'Node::Node(int, boost::shared_ptr)':
main.cpp:9:34: error: use of deleted function 'boost::shared_ptr::shared_ptr(const boost::shared_ptr&)'
In file included from /usr/include/boost/shared_ptr.hpp:17:0,
from main.cpp:2:
/usr/include/boost/smart_ptr/shared_ptr.hpp:168:25: note: 'boost::shared_ptr::shared_ptr(const boost::shared_ptr&)' is implicitly declared as deleted because 'boost::shared_ptr' declares a move constructor or move assignment operator

问题> 我看到了帖子 Using std::shared_ptr with clang++ 和libstdc++.但是,我不知道如何修复它.

Question> I have see the post Using std::shared_ptr with clang++ and libstdc++. However, I don't know how to fix it.

该问题中发布的解决方案是向 shared_ptr 添加默认的复制构造函数和复制赋值运算符将解决该问题."

The solution posted in that question is "Adding a defaulted copy constructor and copy assignment operator to shared_ptr will fix the problem."

推荐答案

这是一个 错误 在旧版本的 boost::shared_ptr 中,使其与 C++11 编译器不兼容.

This is a bug in older versions of boost::shared_ptr that makes it incompatible with C++11 compilers.

最终的 C++11 标准说声明移动构造函数或移动赋值运算符会阻止复制构造函数的隐式定义,但旧版本的 boost::shared_ptr 不遵守该规则并且假设将隐式定义复制构造函数.

The final C++11 standard says that declaring a move constructor or move assignment operator prevents the implicit definition of a copy constructor, but older versions of boost::shared_ptr do not respect that rule and assume that a copy constructor will be implicitly defined.

您需要升级到 Boost 1.48 或更高版本,或者编辑 Boost 标头以将其添加到 shared_ptr:

You either need to upgrade to Boost version 1.48 or later, or edit the Boost headers to add this to shared_ptr:

shared_ptr(const shared_ptr&) = default;

这篇关于boost::shared_ptr::shared_ptr(const boost::shared_ptr&amp;)' 被隐式声明为已删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:boost::shared_ptr::shared_ptr(const boost::shared_ptr&amp;)' 被隐式声明为已删除

基础教程推荐