gpt4 book ai didi

c++ - 来自 shared_ptr 的类型转换错误

转载 作者:太空宇宙 更新时间:2023-11-04 16:25:19 24 4
gpt4 key购买 nike

我有以下代码:

namespace {
class CatBondEvent
{
public:
CatBondEvent(Date date) : date(date){}
virtual ~CatBondEvent(){}
Date date;

bool operator< (CatBondEvent &other) { return date<other.date;}
};

class CatEvent : CatBondEvent
{
public:
Real loss;
CatEvent(Date date, Real loss) : CatBondEvent(date), loss(loss) {}
};

class CollateralCoupon : CatBondEvent
{
public:
Real unitAmount;
CollateralCoupon(Date date, Real unitAmount) : CatBondEvent(date), unitAmount(unitAmount) {}
};

class CatBondCoupon : CatBondEvent
{
public:
CatBondCoupon(Date date) : CatBondEvent(date) {}
};

void fillEvents(std::vector<boost::shared_ptr<CatBondEvent> > &events,
Date startDate,
Date endDate,
boost::shared_ptr<std::vector<std::pair<Date, Real> > > catastrophes,
boost::shared_ptr<Bond> collateral,
boost::shared_ptr<std::vector<Date> > couponDates)
{
for(int i=0; i<catastrophes->size(); ++i)
{
events.push_back(boost::shared_ptr<CatBondEvent>(new CatEvent((*catastrophes)[i].first, (*catastrophes)[i].second)));
}
const Leg& cashflows = collateral->cashflows();
for(int i=0; i<cashflows.size() && cashflows[i]->date()<=endDate; ++i)
{
boost::shared_ptr<CashFlow> cashflow = cashflows[i];
if(cashflow->date() >= startDate)
{
events.push_back(boost::shared_ptr<CatBondEvent>(new CollateralCoupon(cashflow->date(), cashflow->amount())));
}
}
for(int i=0; i<couponDates->size(); ++i)
{
Date couponDate = (*couponDates)[i];
if(couponDate >= startDate)
{
events.push_back(boost::shared_ptr<CatBondEvent>(new CatBondCoupon(couponDate)));
}
}
}
}

我得到以下编译错误:

Error   37  error C2243: 'type cast' : conversion from '`anonymous-namespace'::CollateralCoupon *' to '`anonymous-namespace'::CatBondEvent *' exists, but is inaccessible   c:\boost\boost_1_49_0\boost\smart_ptr\shared_ptr.hpp    184
Error 36 error C2243: 'type cast' : conversion from '`anonymous-namespace'::CatEvent *' to '`anonymous-namespace'::CatBondEvent *' exists, but is inaccessible c:\boost\boost_1_49_0\boost\smart_ptr\shared_ptr.hpp 184
Error 38 error C2243: 'type cast' : conversion from '`anonymous-namespace'::CatBondCoupon *' to '`anonymous-namespace'::CatBondEvent *' exists, but is inaccessible c:\boost\boost_1_49_0\boost\smart_ptr\shared_ptr.hpp 184

知道哪里出了问题以及如何解决吗?

最佳答案

您的继承是私有(private)的,这可以防止从指向派生类的指针转换为指向其基类的指针。您可能想要公共(public)继承:

class CatEvent : public CatBondEvent
^^^^^^

除非另有说明,对成员和基类的访问在使用 class 关键字定义的类中是私有(private)的,在使用 struct 定义的类中是公共(public)的。

关于c++ - 来自 shared_ptr 的类型转换错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12510476/

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