![](/img/trans.png)
[英]What is boost's shared_ptr(shared_ptr<Y> const & r, T * p) used for?
[英]boost::shared_ptr<> “explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete”
有人嘗試在boost :: smart_ptr內多態返回對象時遇到以下錯誤,可以幫我嗎:
1>C:\Program Files\Boost\boost_1_54_0\boost/smart_ptr/shared_ptr.hpp(352): error : a value of type "PBO *" cannot be used to initialize an entity of type "O*"
1> explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete
這是代碼,第一種方法是發生錯誤的地方。 是因為我缺少復制構造函數或賦值運算符,而boost :: shared_ptr要求將它們定義為“完整”嗎?
CE文件
#include "CE.h"
boost::shared_ptr<OB> CE::getObject(){
//THIS IS WHERE THE ABOVE ERROR OCCURS
return boost::shared_ptr<OB>(new PBO);
}
。
#include "E.h"
#include "PBO.h"
#include <boost\shared_ptr.hpp>
#include <unordered_map>
class CE: public E{
public:
virtual boost::shared_ptr<OB> getObject();
private:
};
h
#include "OB.h"
#include <boost\shared_ptr.hpp>
#include <unordered_map>
class E{
public:
virtual boost::shared_ptr<OB> getObject() = 0;
private:
};
OB
//The parent class in the polymorphic hierarchy:
class OB{
public:
OB();
virtual void c(boost::shared_ptr<OD> lo);
virtual void d(std::unordered_map<double, long> a, std::set<double> b, boost::shared_ptr<OD> o) = 0;
protected:
};
壓力
#include "OD.h"
#include "OB.h"
//The child class in the polymorphic hierarchy:
class PBO : public OB{
public:
PBO();
virtual void c(boost::shared_ptr<OD> l);
private:
virtual void d(std::unordered_map<double, long> a, std::set<double> b, boost::shared_ptr<OD> c);
};
根據錯誤函數boost::shared_ptr<OB> CE::getObject()
僅看到class PBO
前向聲明,而不是定義。 但是由於必須將PBO *
轉換為基本OB *
,因此必須查看類PBO的定義。 解決方法可能是將函數聲明放入標頭中:
class OB; // if you put this function declaration before definition of class OB
boost::shared_ptr<OB> getObject();
並將其實施到cpp文件中,在其中可以同時看到OB
和PBO
定義:
#include "OB.h"
#include "PBO.h"
boost::shared_ptr<OB> CE::getObject(){
return boost::shared_ptr<OB>(new PBO);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.