2012-10-09 27 views
1

我創建了全局函數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" ??? 
}  

} 
+0

這就是爲什麼我總是喜歡到命名空間的縮進內容。 –

回答

2

CallPrice在命名空間CLARK中。因此,嘗試

CLARK::CallPrice(/* ... */); 
+0

哇,謝謝。我多麼愚蠢。將盡快接受:) –

1

您已經聲明在命名空間CLARK全球CallPrice。語法::CallPrice嘗試使用在全局名稱空間或匿名名稱空間中定義的函數CallPrice。相反,使用CLARK::CallPrice

0

您在命名空間CLARK:

return CLARK::CallPrice(T, K, sig, r, b, S);