我创建了一个全局函数 CallPrice(args)。我有一个类 EuropeanOption,还有一个名为 CallPrice 的类函数,它应该使用 EuropeanOption 类中的变量调用全局函数,并返回 CallPrice。我收到一个错误,“全局范围没有“CallPrice”。
我认为这是一个普遍的问题。我搜索了其他线程,其中说添加 :: 应该可以解决问题,但它不适用于我。你能找出错误的原因吗?我是否需要将此作为朋友功能或其他一些解决方法?
谢谢!
标题:
#ifndef EuropeanOption_HPP
#define EuropeanOption_HPP
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <boost/math/distributions/normal.hpp>
using namespace boost::math;
using namespace std;
namespace CLARK
{
struct EquityParms
{
double T; // years until expiry
double K; // strike price
double sig; // vol
double r; // risk free rate
double b; // cost of carry
};
// Global Call function
const double CallPrice(double T, double K, double sig, double r, double b, double EquityPrice);
class EuropeanOption
{
private:
double T; // years until expiry
double K; // strike price
double sig; // vol
double r; // risk free rate
double b; // cost of carry
double S; // current equity price
double ExactCallPrice;
public:
EuropeanOption(); // default constructor (empty)
EuropeanOption(const EquityParms& data, double EquityPrice); // constructor that sets parms
void copy(const EuropeanOption& source);
~EuropeanOption();
void init(const EquityParms& data, double EquityPrice); // initialize EquityParms
const double CallPrice(); // trying to call global function in this function
};
}
#endif
来源:
#include "EuropeanOption_H.hpp"
namespace CLARK
{
const double CallPrice(double T, double K, double sig, double r, double b, double EquityPrice)
{// Global Function
double temp = sig * sqrt(T);
double d1 = (log(EquityPrice / K) + (r + (sig*sig) * 0.5) * T) / temp;
double d2 = d1 - temp;
normal_distribution<> myNormal(0,1);
return (EquityPrice * cdf(myNormal,d1)) - (K * exp((b - r) * T) * cdf(myNormal, d2));
}
EuropeanOption::EuropeanOption()
{// default constructor
cout << "Default constructor call" << endl;
}
EuropeanOption::EuropeanOption(const EquityParms& data, double EquityPrice)
{// constructor that sets parms
init(data, EquityPrice);
}
void EuropeanOption::copy(const EuropeanOption& source)
{
T = source.T;
K = source.K;
sig = source.sig;
r = source.r;
S = source.S;
b = source.b;
}
EuropeanOption::~EuropeanOption()
{
}
void EuropeanOption::init(const EquityParms& data, double EquityPrice)
{
T = data.T;
K = data.K;
sig = data.sig;
r = data.r;
S = EquityPrice;
b = data.b;
}
const double EuropeanOption::CallPrice()
{ // trying to call global function in this function
return ::CallPrice(T, K, sig, r, b, S); // the global scope has no "CallPrice" ???
}
}