C:\Program Files\Boost\boost_1_54_0\boost/smart-6ren">
gpt4 book ai didi

c++ - boost::shared_ptr <> "explicit shared_ptr( Y * p ): px( p ), pn()//Y must be complete"

转载 作者:行者123 更新时间:2023-11-28 07:16:36 24 4
gpt4 key购买 nike

有人可以帮我解决我在 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.cpp

#include "CE.h"

boost::shared_ptr<OB> CE::getObject(){
//THIS IS WHERE THE ABOVE ERROR OCCURS
return boost::shared_ptr<OB>(new PBO);
}

CE.h

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

};

E.h

#include "OB.h"

#include <boost\shared_ptr.hpp>
#include <unordered_map>

class E{

public:

virtual boost::shared_ptr<OB> getObject() = 0;

private:

};

OB.h

//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:

};

PBO.h

#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 的定义。解决方案可能是将函数声明放入 header 中:

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);
}

关于c++ - boost::shared_ptr <> "explicit shared_ptr( Y * p ): px( p ), pn()//Y must be complete",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20131700/

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