gpt4 book ai didi

c++ - 使用 boost::shared_ptr 时出错

转载 作者:行者123 更新时间:2023-11-28 00:46:38 25 4
gpt4 key购买 nike

我正在学习 boost 和智能指针。在编译期间我遇到了一个错误,我无法弄清楚它是关于什么的。我不明白我做错了什么。问题出在构造函数中:

DefaultCreature(const Creature& def) : def_(def) {}

这是我的代码:

#include <iostream>
#include <boost/smart_ptr.hpp>

using namespace std;
using namespace boost;

class Creature;
typedef shared_ptr<Creature> PCreature;

class Creature {
public:
Creature(const string& name) : name_(name) {}
const string& getName() const { return name_; }
private:
string name_;
};

class DefaultCreature {
public:
DefaultCreature(const Creature& def) : def_(def) {}
private:
PCreature def_;
};

int main() {
DefaultCreature factory(Creature("lion"));
return 0;
}

还有一个错误:

exercise1.cpp: In constructor ‘DefaultCreature::DefaultCreature(const Creature&)’:
exercise1.cpp:20:52: error: no matching function for call to ‘boost::shared_ptr<Creature>::shared_ptr(const Creature&)’
exercise1.cpp:20:52: note: candidates are:
In file included from /usr/local/include/boost/shared_ptr.hpp:17:0,
from /usr/local/include/boost/smart_ptr.hpp:21,
from exercise1.cpp:2:
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:472:14: note: template<class Ap> boost::shared_ptr::shared_ptr(Ap, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type)
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:472:14: note: template argument deduction/substitution failed:
/usr/local/include/boost/smart_ptr/shared_ptr.hpp: In substitution of ‘template<class Ap> boost::shared_ptr::shared_ptr(Ap, typename boost::detail::sp_enable_if_auto_ptr<Ap, int>::type) [with Ap = Creature]’:
exercise1.cpp:20:52: required from here
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:472:14: error: no type named ‘type’ in ‘struct boost::detail::sp_enable_if_auto_ptr<Creature, int>’
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:446:14: note: template<class Y> boost::shared_ptr::shared_ptr(std::auto_ptr<_Tp1>&)
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:446:14: note: template argument deduction/substitution failed:
exercise1.cpp:20:52: note: types ‘std::auto_ptr<T>’ and ‘const Creature’ have incompatible cv-qualifiers

(...)

/usr/local/include/boost/smart_ptr/shared_ptr.hpp:339:5: note: boost::shared_ptr<T>::shared_ptr() [with T = Creature]
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:339:5: note: candidate expects 0 arguments, 1 provided
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:328:25: note: boost::shared_ptr<Creature>::shared_ptr(const boost::shared_ptr<Creature>&)
/usr/local/include/boost/smart_ptr/shared_ptr.hpp:328:25: note: no known conversion for argument 1 from ‘const Creature’ to ‘const boost::shared_ptr<Creature>&’

最佳答案

shared_ptr 的参数必须是动态分配对象的地址,但代码传递的是一个引用。更改为,例如:

class DefaultCreature {
public:
DefaultCreature(const Creature& def) : def_(new Creature(def)) {}
private:
PCreature def_;
};

或使用 boost::make_shared :

class DefaultCreature {
public:
DefaultCreature(const Creature& def) :
def_(boost::make_shared<Creature>(def)) {}
private:
PCreature def_;
};

如果 DefaultCreature 的实例是唯一可以访问 def_ 指向的对象的对象,那么它就没有理由成为 boost::shared_ptr:使用boost::scoped_ptr反而。参见 What C++ Smart Pointer Implementations are available?非常有用的智能指针概述。


但是,从发布的代码来看,似乎没有理由使用任何性质的指针。只需在 DefaultCreature 中存储一个 Creature 实例(根据发布的代码,Creature 是可复制的,没有多态要求)。

关于c++ - 使用 boost::shared_ptr 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15987070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com