本文介绍了提高:: shared_ptr的:: shared_ptr的(常量的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

问题>我有看到帖子。但是,我不知道如何解决它。

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指出,声明移动构造函数或移动赋值操作符prevents拷贝构造函数的隐式定义,但旧的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.

您要么需要升级到版本提升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;

这篇关于提高:: shared_ptr的:: shared_ptr的(常量的boost :: shared_ptr的&安培;)“是隐式声明为已删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-01 13:09