簡體   English   中英

boost :: shared_ptr <>“顯式shared_ptr(Y * p):px(p),pn()// Y必須完整”

[英]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文件中,在其中可以同時看到OBPBO定義:

#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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM