没有合适的默认构造函数 - 默认构造函数在哪里调用?

时间:2016-07-18 14:36:07

标签: c++ default-constructor

编译下面的代码我得到了第105行error C2512: 'PayOffBridge': no appropriate default constructor available

我的问题是:调用默认构造函数的位置?我该如何解决?

注意:这里提供的大部分代码来自Mark Joshi - 设计模式和衍生品定价

#include <iostream>
#include <boost/random/mersenne_twister.hpp>
#include <boost/random/normal_distribution.hpp>
#include <boost/random/variate_generator.hpp>
#include <cmath>
#include <boost/shared_ptr.hpp>
#include <stdio.h>
#include <vector>

// *********************  PayOff3

class PayOff{
    public:
        PayOff(){};
        virtual double operator()(double Spot) const = 0 ;
        virtual PayOff* clone() const = 0;
        virtual ~PayOff(){}

    private:
};


class PayOffCall : public PayOff{
    public:
        PayOffCall(double Strike_);
        virtual double operator()(double Spot) const ;
        virtual ~PayOffCall(){}
        virtual PayOff* clone () const ;
    private:
        double Strike;
};

PayOffCall::PayOffCall(double Strike_) : Strike(Strike_) {

}

double PayOffCall::operator()(double Spot) const {
    return std::max(Spot - Strike, 0.0 );
}

PayOff* PayOffCall::clone () const {
    return new PayOffCall(*this);
}

// ********************* PayOff Bridge

class PayOffBridge {
    public :
        PayOffBridge(const PayOffBridge& original);
        PayOffBridge(const PayOff& innerPayOff);
        PayOffBridge& operator = (const PayOffBridge& original);
        ~PayOffBridge();

        inline double operator()(double Spot) const;

    private:
        PayOff* ThePayOffPtr;
};

inline double PayOffBridge::operator() (double Spot) const {
    return ThePayOffPtr->operator()(Spot);
}

PayOffBridge::PayOffBridge(const PayOffBridge& original){
    ThePayOffPtr = original.ThePayOffPtr->clone();
}

PayOffBridge::PayOffBridge(const PayOff& innerPayOff){
    ThePayOffPtr = innerPayOff.clone();
}

PayOffBridge& PayOffBridge::operator = (const PayOffBridge& original){
    if (this != &original){
        delete ThePayOffPtr;
        ThePayOffPtr = original.ThePayOffPtr->clone();
    }
    return *this;
}

PayOffBridge::~PayOffBridge(){
    delete ThePayOffPtr;
}

// *********************  Vanilla2

class VanillaOption {

    public:
        VanillaOption(const PayOffBridge& thePayOff, double expiry);
        VanillaOption(const VanillaOption& original);
        VanillaOption& operator() (const VanillaOption& original);
        ~VanillaOption();

        double GetExpiry() const ;
        double OptionPayOff(double Spot) const ;

    private:
        double Expiry;
        PayOffBridge ThePayOffBridge;
};

VanillaOption::VanillaOption(const PayOffBridge& thePayOff, double expiry) : Expiry(expiry), ThePayOffBridge(thePayOff){
}

VanillaOption::VanillaOption(const VanillaOption& original){
    Expiry=original.GetExpiry();
    ThePayOffBridge = original.ThePayOffBridge;
}


double VanillaOption::GetExpiry() const {
    return Expiry;
}
double VanillaOption::OptionPayOff(double Spot) const {
    return ThePayOffBridge(Spot);
}

VanillaOption& VanillaOption::operator() (const VanillaOption& original){
    if (this != &original){
        Expiry=original.GetExpiry();
        ThePayOffBridge = original.ThePayOffBridge;
    }
    return *this;
}

VanillaOption::~VanillaOption(){
}



int main (){

}

1 个答案:

答案 0 :(得分:4)

VanillaOption(const VanillaOption& original)没有初始化列表,这意味着成员必须是默认构造的。做到这一点

VanillaOption::VanillaOption(const VanillaOption& original)
  : Expiry(original.GetExpiry()),
    ThePayOffBridge(original.ThePayOffBridge)
{}