QuantLib 错误:"QuantLib::Handle<QuantLib::TermStructure>"无法转换为"const QuantLib::Handle<QuantLi



我正在C++中使用QuantLib 1.19。当我试图传递一个TermStructure句柄来创建一个新的IborIndex时,我在主题中得到了错误。我的代码看起来像:

#include <iostream>
#include <ql/quantlib.hpp>
#include <vector>
int main() {
using namespace std;
using namespace QuantLib;
Date today(21, Apr, 2021);
std::string familyName("TestTest");
Period tenor(1, Years);
Natural settlementDays(0);
USDCurrency usd;
Currency currency(usd);
TARGET target;
BusinessDayConvention convention(ModifiedFollowing);
bool endOfMonth(true);
Actual365Fixed dayCounter;
ext::shared_ptr<YieldTermStructure> crv(new FlatForward(today, 0.03, dayCounter));
Handle<TermStructure> crv_handle(crv);
IborIndex crv_index(familyName, tenor, settlementDays, currency, target, convention, endOfMonth, dayCounter, crv_handle);

return EXIT_SUCCESS;
}

我正在检查1.19中IborIndex的定义,它是:

class IborIndex : public InterestRateIndex {
public:
IborIndex(const std::string& familyName,
const Period& tenor,
Natural settlementDays,
const Currency& currency,
const Calendar& fixingCalendar,
BusinessDayConvention convention,
bool endOfMonth,
const DayCounter& dayCounter,
const Handle<YieldTermStructure>& h =
Handle<YieldTermStructure>());

但在最新版本1.22中,它删除了termstructure句柄参数中的常量:

class IborIndex : public InterestRateIndex {
public:
IborIndex(const std::string& familyName,
const Period& tenor,
Natural settlementDays,
const Currency& currency,
const Calendar& fixingCalendar,
BusinessDayConvention convention,
bool endOfMonth,
const DayCounter& dayCounter,
Handle<YieldTermStructure> h = Handle<YieldTermStructure>());

我是C++新手。所以,也许这真的是一个基本C++的问题。但你能帮我理解1.19中出现这个错误的原因吗?为什么现在1.22发生了变化?

非常感谢。

构造函数参数特别要求您传递一个Handle<YieldTermStructure>

但是,您正在传递一个Handle<TermStructure>,这是一个不兼容的类型,这就是您收到错误的原因。

我不知道这个库,但我希望你能通过声明来纠正它

Handle<YieldTermStructure> crv_handle(crv);

1.22中发生的变化与此无关。该参数的参数传递方法已从常量引用更改为值。你仍然会得到同样的错误。

最新更新