我有一个相当简单的C ++代码,似乎没有正确编译。基本上,我在GLOBAL.HPP文件中声明了一些全局定义的函数,并在我的GLOBAL.CPP文件中定义。然后我有一个类,EuroOption,它由一个struct datamember组成。 EuroOption类有自己的成员函数,它们基本上与全局函数完全相同 - 所以我对它们进行了类似的定义,并且只调用了EuroOption成员函数定义中的全局函数。请参阅以下内容:
//
//GLOBAL.HPP
//
#ifndef GLOBAL_HPP
#define GLOBAL_HPP
#include <iostream>
#include <math.h>
#include <boost/math/distributions/normal.hpp>
#include <boost/math/distributions.hpp> // For non-member functions of distributions
using namespace std;
//using namespace boost::math;
namespace GLOBAL // Encapsulate Point in the Global namespace
{
struct EuroOptionData
{
double r; // Interest rate
double sig; // Volatility
double K; // Strike price
double T; // Expiry date
double b; // Cost of carry
};
double n(double x);
double N(double x);
double CallPrice(EuroOptionData od, double S);
double PutPrice(EuroOptionData od, double S);
double PutParity(EuroOptionData od, double S);
double CallParity (EuroOptionData od, double S);
} // Close namespace GLOBAL
#endif
这是EuroOption.HPP文件:
//
//
//
#ifndef EUROOPTION_HPP
#define EUROOPTION_HPP
#include <string>
#include "Global.hpp"
using namespace std;
using namespace GLOBAL;
class EuroOption
{
private:
public:
struct EuroOptionData od;
//EuroOption class functions
EuroOption(); // Default call option
EuroOption(const EuroOption& option2); // Copy constructor
virtual ~EuroOption(); //Destructor
//EuroOption Global Function Calls
double EuroCallPrice(EuroOptionData od, double S);
double EuroPutPrice(EuroOptionData od, double S);
double EuroCallParity(EuroOptionData od, double S);
double EuroPutParity(EuroOptionData od, double S);
//EuroOption class operators
EuroOption& operator = (const EuroOption& option2); //Assignment Operator
};
#endif
以及EuroOption.CPP文件的片段:
//
//
//
#include "EuroOption.hpp"
#include <cmath>
#include <iostream>
using namespace GLOBAL;
{
double EuroOption::EuroCallPrice(EuroOptionData od, double S)
{
return CallPrice(od,S);
};
double EuroOption::EuroPutPrice(EuroOptionData od, double S)
{
return CallPrice(od,S);
};
.....
...
}
最后,我的Test.CPP文件的片段,我在其中测试功能:
//
//
//
#include "Global.hpp"
#include "EuroOption.hpp"
#include <iostream>
using namespace GLOBAL;
int main()
{
EuroOption Batch1; //Initialize EuroOption class object Batch1
cout << "S1: "; double S1; cin >> S1;
cout << "Stock Call Option: " << EuroCallPrice(Batch1.od, S1) << endl;
cout << "Stock Put Option: " << EuroPutPrice(Batch1.od, S1) <<endl;
cout << "Put Call Parity - Call Option:"<< EuroCallParity(Batch1.od, S1)<<endl;
cout << "Put Call Parity - Put Option: "<< EuroPutParity(Batch1.od, S1)<<endl;
//****None of these functions compile. They all state "identifier EuroCallPrice (..etc.) is undefined."
cout << "S1: "; double S1; cin >> S1;
cout << "Stock Call Option: " << CallPrice(Batch1.od, S1) << endl;
cout << "Stock Put Option: " << PutPrice(Batch1.od, S1) <<endl;
cout << "Put Call Parity - Call Option:"<< CallParity(Batch1.od, S1)<<endl;
cout << "Put Call Parity - Put Option: "<< PutParity(Batch1.od, S1)<<endl;
//****These functions all compile properly. They are the original global functions.
我意识到这是需要筛选的大量代码,但任何想法都会受到高度赞赏。如上面的代码所述,原始的全局函数完美地工作,但我想使用类EuroOption函数来调用该全局函数。
非常感谢!
答案 0 :(得分:0)
感谢您的帮助!