[英]Error while using boost::shared_ptr
我正在學習加速和智能指針。 在編譯期間,我遇到了一個錯誤,並且我無法弄清楚這是怎么回事。 我不明白我在做什么錯。 問題出在構造函數中:
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
。 請參閱可用的C ++智能指針實現? 有關智能指針的非常有用的概述。
但是,從發布的代碼來看,似乎沒有理由使用任何性質的指針。 只需將一個Creature
實例存儲在DefaultCreature
(根據發布的代碼, Creature
是可復制的,並且不需要多態)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.