c++ - C++ 中指针的困难/链接错误

标签 c++ boost linker-errors shared-ptr quantlib

编译我的代码时出现以下错误:

LNK2019 unresolved external symbol "public: class QuantLib::Matrix const & __cdecl SachLib::AWDCalculator::RSB(void)const " (?RSB@AWDCalculator@SachLib@@QEBAAEBVMatrix@QuantLib@@XZ) referenced in function "protected: void __cdecl SachLib::CalculationEngine_Sach::set_results(class boost::shared_ptr &)const " (?set_results@CalculationEngine_Sach@SachLib@@IEBAXAEAV?$shared_ptr@VAWDCalculator@SachLib@@@boost@@@Z)

它位于第 1 行的 CalculationEngine_Sach.obj 中。

这是我的代码:

Abwicklungsdreieck 和 Gewichtungsfaktoren 基本上由一个 Matrix 组成,并由其各自的 Reader 获得。我认为他们的方法的名称是明确的。

主要.cpp:

#include "AbwicklungsdreieckReader.h"
#include "Abwicklungsdreieck.h"
#include "GewichtungsfaktorenReader.h"
#include "Gewichtungsfaktoren.h"
#include "Tarif_Sach.h"
#include "CalculationEngine_Sach.h"
#include "CalculationEngine_Sach_Typ1.h"


#if defined(QL_ENABLE_SESSIONS)
namespace QuantLib {
    Integer sessionId() { return 0; }
}
#endif

using namespace QuantLib;
using namespace SachLib;


int main()
{
    std::cout << "\n\n\nTest Chain-Ladder-Verfahren\n\n";

    std::string input1 = "C:/Users/D91476/Desktop/LifeLib_Sol/awd.csv";
    std::string input2 = "C:/Users/D91476/Desktop/LifeLib_Sol/gwf.csv";


    boost::shared_ptr<PricingEngine> EngineTyp1(new CalculationEngine_Sach_Typ1());

    SachLib::AbwicklungsdreieckReader input1Reader(input1);
    SachLib::GewichtungsfaktorenReader input2Reader(input2);

    SachLib::Abwicklungsdreieck AWD = input1Reader.get_awd();
    std::cout << "\nInput 1: Abwicklungsdreieck\n\n";
    std::cout << AWD.get_Matrix() << "\n";

    SachLib::Gewichtungsfaktoren GWF = input2Reader.get_gwf();
    std::cout << "\nInput 2: Gewichtungsfaktoren\n\n";
    std::cout << GWF.get_Matrix();

    Tarif_Sach *tarifsach1;
    tarifsach1 = new Tarif_Sach_Typ1(AWD, GWF);

    tarifsach1->setPricingEngine(EngineTyp1);
    EngineTyp1->calculate();
}

通过测试我发现错误的出现是因为下面一行:

boost::shared_ptr<PricingEngine> EngineTyp1(new CalculationEngine_Sach_Typ1());

AWDCalculator.h

#pragma once

#include "Tarif_Sach.h"
#include "Abwicklungsdreieck.h"
#include "Gewichtungsfaktoren.h"

#ifndef _AWDCalculator_H
#define _AWDCalculator_H

namespace SachLib {

    class AWDCalculator {
    public:
        AWDCalculator();
        AWDCalculator(Tarif_Sach::arguments *arguments);
        AWDCalculator(Abwicklungsdreieck awd, Gewichtungsfaktoren gwf);
        AWDCalculator(const AWDCalculator &obj);
        AWDCalculator& operator=(AWDCalculator const& rhs);


        //! Fills vectors.
        void update(Tarif_Sach::arguments *arguments);

        // Logic
        void calculate(Abwicklungsdreieck awd, Gewichtungsfaktoren gwf);

        //! \name Getter
        //@{
        const Abwicklungsdreieck& awd() const;
        const Gewichtungsfaktoren& gwf() const;
        const Matrix& estimated_costs_matrix() const;
        const Matrix& RSB_all() const;
        const Matrix& RSB() const;
        //@}

        Tarif_Sach::arguments *arguments_;

    protected:
        AWDCalculator *results_;
        Abwicklungsdreieck awd_;
        Gewichtungsfaktoren gwf_;

        //!  \name Outputs
        //@{
        Matrix estimated_costs_matrix_;
        Matrix RSB_all_;
        Matrix RSB_;
        //@}

    };

}

#endif

AWD计算器.cpp

#include "AWDCalculator.h"


SachLib::AWDCalculator::AWDCalculator()
{
}

SachLib::AWDCalculator::AWDCalculator(Tarif_Sach::arguments *arguments)
    : AWDCalculator(arguments->awd, arguments->gwf)
{
    arguments_ = arguments;
}

SachLib::AWDCalculator::AWDCalculator(Abwicklungsdreieck awd, Gewichtungsfaktoren gwf)
    : awd_(awd),
    gwf_(gwf)
{
    results_ = new AWDCalculator(awd, gwf);
    calculate(awd, gwf);
}

SachLib::AWDCalculator::AWDCalculator(const AWDCalculator& obj)
{
    arguments_ = obj.arguments_;
    awd_ = obj.awd_;
    gwf_ = obj.gwf_;
}

SachLib::AWDCalculator& SachLib::AWDCalculator::operator=(AWDCalculator const& rhs)
{
    arguments_ = rhs.arguments_;
    awd_ = rhs.awd_;
    gwf_ = rhs.gwf_;

    return *this;
}


void SachLib::AWDCalculator::update(Tarif_Sach::arguments *arguments)
{
    results_ = new AWDCalculator(arguments);
    arguments_ = arguments;
    awd_ = arguments->awd;
    gwf_ = arguments->gwf;
}

void SachLib::AWDCalculator::calculate(Abwicklungsdreieck awd, Gewichtungsfaktoren gwf)
{
    // RSB_, RSB_all_ and estimated_costs_matrix_ get calculated here with the input awd and gwf
}

inline const Matrix& SachLib::AWDCalculator::estimated_costs_matrix() const
{
    return estimated_costs_matrix_;
}

inline const Matrix& SachLib::AWDCalculator::RSB_all() const
{
    return RSB_all_;
}

inline const Matrix& SachLib::AWDCalculator::RSB() const
{
    return RSB_;
}

inline const SachLib::Abwicklungsdreieck& SachLib::AWDCalculator::awd() const
{
    return awd_;
}

inline const SachLib::Gewichtungsfaktoren& SachLib::AWDCalculator::gwf() const
{
    return gwf_;
}

计算引擎_Sach.h

#pragma once

#ifndef SachLib_calculationengine_sach_hpp
#define SachLib_calculationengine_sach_hpp

#include "Tarif_Sach.h"
#include "AWDCalculator.h"

using namespace QuantLib;

namespace SachLib {

    class CalculationEngine_Sach : public Tarif_Sach::engine {
    public:
        CalculationEngine_Sach();
        void calculate() const;

    protected:
        mutable boost::shared_ptr<AWDCalculator> AWDCalculator_;

        void set_results(boost::shared_ptr<AWDCalculator> &awdCalculator) const;

    };

}

#endif

计算引擎_Sach.cpp

#include "CalculationEngine_Sach.h"
#include "Tarif_Sach.h"
#include "AWDCalculator.h"


using namespace QuantLib;

namespace SachLib {

    CalculationEngine_Sach::CalculationEngine_Sach()
    {

    }

    void CalculationEngine_Sach::calculate() const
    {
        arguments_.AWDCalculator->update(&arguments_);

        CalculationEngine_Sach::set_results(arguments_.AWDCalculator);
    }

    void CalculationEngine_Sach::set_results(boost::shared_ptr<AWDCalculator> &awdCalculator) const
    {
        results_.estimated_costs_matrix = awdCalculator->estimated_costs_matrix();
        results_.RSB_all = awdCalculator->RSB_all();
        results_.RSB = awdCalculator->RSB();
        // because of the above three lines the error appears
    }
}

CalculationEngine_Sach_Typ1.h

#pragma once
#include "CalculationEngine_Sach.h"

namespace SachLib {
    class CalculationEngine_Sach_Typ1 : public CalculationEngine_Sach
    {
    public:
        CalculationEngine_Sach_Typ1();
    };

}

CalculationEngine_Sach_Typ1.cpp

#include "CalculationEngine_Sach_Typ1.h"

SachLib::CalculationEngine_Sach_Typ1::CalculationEngine_Sach_Typ1()
{}

如果有人能提供帮助,我将不胜感激。

最佳答案

您在 .cpp 中将该函数标记为 inline:

inline const Matrix& SachLib::AWDCalculator::RSB() const

在这种情况下,编译器不必生成外联定义,除非在该翻译单元中需要它(例如,采用函数地址)。这会导致链接器错误。

从函数定义中删除inline,使编译器生成一个外联定义来修复链接器错误。

关于c++ - C++ 中指针的困难/链接错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47093314/

相关文章:

c++ - 为什么专门化 type_trait 会导致未定义的行为?

c++ - 如何使用 Boost.Test 捕获我的自定义异常?

c++ - 在到达范围结束后 boost 共享指针 "runtime error"

ios - ld : library not found for -lyoutube-ios-player-helper

c++ - 交叉编译SFML时对libpng相关符号的 undefined reference

c++ - 如何授予对私有(private)方法的访问权限

c++ - OpenMP 原子 _mm_add_pd

boost - 通过串行端口的 OpenSSL

xcode - Xcode 4 的 "bad codegen, pointer diff"链接器错误

c++ - OpenGL 窗口显示背景窗口中的内容而不是空白窗口