提升::shared_ptr<> "explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete"
boost::shared_ptr<> "explicit shared_ptr( Y * p ): px( p ), pn() // Y must be complete"
当我试图在boost中以多态方式返回对象时,有人能帮我解决以下错误吗::smart_ptr:
1>C:Program FilesBoostboost_1_54_0boost/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 <boostshared_ptr.hpp>
#include <unordered_map>
class CE: public E{
public:
virtual boost::shared_ptr<OB> getObject();
private:
};
E.h
#include "OB.h"
#include <boostshared_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的定义。解决方案可以是将函数声明放入头中:
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);
}
相关文章:
- CLANG 编译器 说:变量"PTR"可能未初始化
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 引用 std::shared:ptr 以避免引用计数
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- 尝试打印出 *ptr++ 的值,以了解它是如何工作的
- 如何控制共享 ptr 引用计数?
- dopen():不以 root 身份运行时"failed to map segment from shared object"
- C++中的指针否定 (!ptr == NULL)
- 从const ptr*转换为ptr*时出现问题
- 无法使用 libtool 将 -shared 参数传递给 g++
- boost::shared_ptr和std::shared-ptr的同居
- 我可以用std::shared_ptr而不是boost::shared-ptr构建boost库吗
- shared-ptr-C++shared_ptr与unique_ptr用于资源管理