C ++:从QuantLib :: PiecewiseYieldCurve

时间:2015-08-28 13:36:55

标签: quantlib

我想从QuantLib :: PiecewiseYieldCurve派生一个类模板。这是我的派生类定义:

#include <ql\termstructures\yield\piecewiseyieldcurve.hpp>
#include <MarKetQuote\include\OISQuote.h>
#include <vector>

using namespace QuantLib;

/*! \brief Class to bootstrap an OIS curve.
*/
template <class Traits, class Interpolator,
    template <class> class Bootstrap = IterativeBootstrap>
class OISCurve : public PiecewiseYieldCurve < Traits, Interpolator, Bootstrap >
{
private:
    /** Typedef to refer to the specialized class.
    It was necessary to add it in other to be able to declare the constructor as in QuantLib::PiecewiseYieldCurve.
    */
    typedef OISCurve<Traits, Interpolator, Bootstrap> this_ois_curve;
public:
    /** Constructor.
    */
    OISCurve(
        const Date& referenceDate,
        const std::vector<OISQuote>& quotes,
        const DayCounter& dayCounter,
        Real accuracy,
        const Interpolator& i = Interpolator(),
        const Bootstrap<this_ois_curve>& bootstrap = Bootstrap<this_ois_curve>());
};

OISQuote只是一个派生自QuantLib :: SimpleQuote的类。我不熟悉这种复杂的模板,但是由于我的理解有限,上面应该没问题。它实际编译。当我尝试像这样实例化一个专门的类时会出现问题:

OISCurve<Discount, LogLinear> oisCurve = OISCurve<Discount, LogLinear>(Date::todaysDate(), quotes, Actual360(), 1.0e-12);

我收到链接错误LNK2019(使用visual studio):

main.obj : error LNK2019: unresolved external symbol "public: __cdecl OISCurve<struct QuantLib::Discount,class QuantLib::LogLinear,class QuantLib::IterativeBootstrap>::OISCurve<struct QuantLib::Discount,class QuantLib::LogLinear,class QuantLib::IterativeBootstrap>(class QuantLib::Date const &,class std::vector<class OISQuote,class std::allocator<class OISQuote> > const &,class QuantLib::DayCounter const &,double,class QuantLib::LogLinear const &)" (??0?$OISCurve@UDiscount@QuantLib@@VLogLinear@2@VIterativeBootstrap@2@@@QEAA@AEBVDate@QuantLib@@AEBV?$vector@VOISQuote@@V?$allocator@VOISQuote@@@std@@@std@@AEBVDayCounter@2@NAEBVLogLinear@2@@Z) referenced in function main
1>C:\Users\u8998\PricingEngine\build\Debug\main.exe : fatal error LNK1120: 1 unresolved externals

感谢您的帮助。

修改

很抱歉迟到的回复。感谢@LuigiBallabio和@StudentT的答案。我的问题最终是由于我对类模板的经验不足造成的。我在头文件中声明了我的声明,我在cpp文件中的实现与任何其他类完全一样。按照this question接受的答案中的指导原则,我相应地修改了我的代码:

OISCurve.h

#ifndef OIS_CURVE_H
#define OIS_CURVE_H

#include <ql\termstructures\yield\piecewiseyieldcurve.hpp>
#include <ql\termstructures\yield\ratehelpers.hpp>
#include <ql\quote.hpp>
#include <vector>
#include <boost\shared_ptr.hpp>

using namespace QuantLib;


template <class Traits, class Interpolator,
    template <class> class Bootstrap = IterativeBootstrap>
class OISCurve : public PiecewiseYieldCurve < Traits, Interpolator, Bootstrap >
{
public:

    OISCurve(const Date& referenceDate,
        const std::vector<boost::shared_ptr<Quote>>& quotes,
        const DayCounter& dayCounter,
        Real accuracy);

    friend std::vector<boost::shared_ptr<RateHelper>> getRateHelpers(const std::vector<boost::shared_ptr<Quote>>& quotes);

private:
    std::vector<boost::shared_ptr<Quote>> quotes_;
};

#include <CurveBootstrapping\src\OISCurve.cpp>

#endif

OISCurve.cpp

#include "OISCurve.h"
#include <ql\errors.hpp>
#include <algorithm>
#include <boost\foreach.hpp>
#include <ql\termstructures\yield\oisratehelper.hpp>
#include <MarKetQuote\include\OISQuote.h>
#include <ql\handle.hpp>
#include <ql\indexes\ibor\eonia.hpp>

#ifndef OIS_CURVE_IMPL
#define OIS_CURVE_IMPL

template <class T, class I, template <class> class B>
OISCurve<T, I, B>::OISCurve(
    const Date& referenceDate,
    const std::vector<boost::shared_ptr<Quote>>& quotes,
    const DayCounter& dayCounter,
    Real accuracy)
    : PiecewiseYieldCurve < T, I, B >(referenceDate, 
    initializeQuotesAndGetRateHelpers(quotes), 
    dayCounter, 
    accuracy)
{
    std::cout << "constructor works" << std::endl;
}

std::vector<boost::shared_ptr<RateHelper>> getRateHelpers(const std::vector<boost::shared_ptr<Quote>>& quotes)
{

    QL_REQUIRE(std::all_of(quotes.begin(), quotes.end(), [](boost::shared_ptr<Quote> quote){ return boost::dynamic_pointer_cast<OISQuote>(quote) != 0; }),
        "All quotes must be OISQuotes!");

    std::vector<boost::shared_ptr<RateHelper>> rateHelpers;
    BOOST_FOREACH(boost::shared_ptr<Quote> quote, quotes)
    {
        rateHelpers.push_back(boost::shared_ptr<RateHelper>(new OISRateHelper(2,
            boost::dynamic_pointer_cast<OISQuote>(quote)->tenor(),
            Handle<Quote>(quote),
            boost::shared_ptr<OvernightIndex>(new Eonia()))));
    }

    return rateHelpers;
}

#endif

这是我最喜欢的解决方案。

1 个答案:

答案 0 :(得分:1)

我根本不认为这些问题与量化金融有关,但无论如何我都会回答。

您尚未向OISCurve类的构造函数提供实现。

要关联您的计划,请执行以下操作:

OISCurve(
    const Date& referenceDate,
    const std::vector<OISQuote>& quotes,
    const DayCounter& dayCounter,
    Real accuracy,
    const Interpolator& i = Interpolator(),
    const Bootstrap<this_ois_curve>& bootstrap = Bootstrap<this_ois_curve>())
: <Call Parent Constructor>
{

}

您需要替换为对PiecewiseYieldCurve中定义的实际构造函数的调用。有六个这样的构造函数,因此您需要选择一个您认为最合适的构造函数。请注意,您需要为父构造函数提供一个工具列表,但它在构造函数定义中缺失。