我正在制作一个用虚拟副本构造函数定义某些类的标题,代码如下:
#ifndef PAYOFF_H
#define PAYOFF_H
class Pay_Off{
public:
// enum Option_Type {call, put};
Pay_Off(/*double Strike_, Option_Type Option_*/){};
virtual double operator()(double Spot) const=0;
virtual ~Pay_Off(){}
virtual Pay_Off* clone() const=0;
private:
};
class Pay_Off_Call: public Pay_Off{
public:
Pay_Off_Call(double Strike_);
virtual double operator()(double Spot) const;
virtual Pay_Off* clone() const;
virtual ~Pay_Off_Call(){}
private:
double Strike;
};
class Pay_Off_Put:public Pay_Off{
public:
Pay_Off_Put(double Strike_);
virtual double operator()(double Spot) const;
virtual Pay_Off* clone const;
virtual ~Pay_Off_Put(){}
private:
double Strike;
};
#endif
编译时不断弹出错误:
error: ‘clone’ declared as a ‘virtual’ field
virtual Pay_Off* clone const;
我不了解它为什么会发生,我该如何解决?
答案 0 :(得分:0)
在班级Pay_Off_Put
virtual Pay_Off* clone const;
您在方法声明中错过了()
。没有它,编译器会将其视为成员变量,它不能是虚拟的。因此它显示了这个错误。
将其声明为
virtual Pay_Off* clone() const;
答案 1 :(得分:0)
将virtual Pay_Off* clone const;
替换为virtual Pay_Off* clone() const;
并进行编译。