Can't access Global Function from within Class - c++

I've created a global function, CallPrice(args). I have a class, EuropeanOption, and I have a class function called CallPrice, which should call the global function using variables from the EuropeanOption class, and return the CallPrice. I'm getting an error, "the global scope has no "CallPrice".
I think this is a common problem. I searched other threads, which said adding :: should solve the problem, but it's not working here for me. Could you identify the cause of the error? Do I need to make this a friend function or some other workaround?
Thanks!
Header:
#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
Source:
#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" ???
}
}

CallPrice is in namespace CLARK. So try
CLARK::CallPrice(/* ... */);

You have declared the global CallPrice in the namespace CLARK. The syntax ::CallPrice tries to use a function CallPrice defined in the global namespace, or an anonymous namespace. Instead, use CLARK::CallPrice.

You are in the namespace CLARK:
return CLARK::CallPrice(T, K, sig, r, b, S);

Related

C++ How to call a constructor parameter inside a class method

So I'm a total noob at C++, I decided to learn C++ and skipped directly to the Object-oriented programming. I'm coding a class called KineticEnergy that has a constructor with the parameters x and y which is assigned to the variables mass and velocity.
I have a class method called result() which calculates the Kinetic Energy using its formula. I want to call the parameters from my constructor within the formula but I have no idea what I'm exactly doing here (bad english, don't know how to explain). I am getting errors like "[Error] x was not declared in this scope". Here is the code I written:
#include <iostream>
#include <cmath>
using namespace std;
class KineticEnergy
{
public:
double mass;
double velocity;
KineticEnergy(double x, double y) {
mass = x;
velocity = y;
}
double result()
{
return (1/2) * (x * (pow(y, 2)));
} // What am I gonna do here for this to work?
};
int main()
{
double a = 12.1;
double b = 6.4;
KineticEnergy ke(a, b);
cout << ke.result();
return 0;
}
It is not necessary. your constructor parameters is saved in "mass" and "velocity" as class members.
double result()
{
return (1./2.) * (mass * (pow(velocity , 2.)));
}
Parameters of the parameterized constructor are not member variables. That's why you are storing param values in member variables inside of the parameterized constructor. So that, you should use member variables inside of the result() function.
try this
#include <iostream>
#include <cmath>
using namespace std;
class KineticEnergy
{
public:
double mass;
double velocity;
KineticEnergy(double x, double y) {
mass = x;
velocity = y;
}
double result()
{
return 0.5 * (mass * pow(velocity, 2));
}
};
int main()
{
double a = 12.1;
double b = 6.4;
double Result;
KineticEnergy ke(a, b);
Result = ke.result();
cout << Result;
}
x and y were declared in your constructor, therefore only known by your constructor. you cannot use them outside of it. however, mass and velocity are known variables of your class and can be used anywhere as long as they are public.
in your main you give mass and velocity of your ke object values, that's why you can call any method of your class that uses these variables after(again, as long as they're public)

Passing function of multiple arguments that contains class to other function

I'm trying to pass function of multiple arguments to other function. I know how to pass a function of single argument function to other function as it was described in C++ primer plus book.
However, I get an error when I'm trying to pass multiple arguments with class(poly_3d) to NR_method function.
#include <iostream>
#define log(x) std::cout<<x<<std::endl;
class constants {
public:
double A;
double B;
double C;
};
double poly_3d(double x, constants cst);
double NR_method(double a, double(*poly_3d)(double));
int main() {
constants cst;
cst.A = 2;
cst.B = -8;
cst.C = 10;
NR_method(3.2, poly_3d);
system("PAUSE");
return 0;
}
double poly_3d(double x, constants cst) {
double y = 3 * cst.A*x*x + 2 * cst.B*x + cst.C;
return y;
}
double NR_method(double a, double (*poly_3d)(double)) {
double c = (*poly_3d)(a);
return c;
}
So the error I'm getting is from NR_method(3.2, poly_3d) in main function. I know that if poly_3d was single arg, this would work.
If this is a horrible way to write codes, then any directions towards learning C++ more effectively for newbies would be much appreciated! Thanks
Take a look at the following code. We're using a template to make things look nicer.
#include <iostream>
#define log(x) std::cout<<x<<std::endl;
class constants {
public:
double A;
double B;
double C;
};
/// Note that we take a ref now, no need to copy cst.
double poly_3d(double x, constants & cst)
{
double y = 3 * cst.A*x*x + 2 * cst.B*x + cst.C;
return y;
}
/// Note that we take a ref now, no need to copy cst.
template <class F>
double NR_method(double a, constants & cst, F func)
{
return func(a, cst);
}
int main() {
constants cst;
cst.A = 2;
cst.B = -8;
cst.C = 10;
NR_method(3.2, cst, &poly_3d);
system("PAUSE");
return 0;
}
You are declaring the function poly_3d with 2 arguments but passing only one. I made a few changes on the code for you
#include <iostream>
#define log(x) std::cout<<x<<std::endl;
class constants {
public:
double A;
double B;
double C;
};
double poly_3d(double x, constants cst);
double NR_method(double a, constants cst, double(*poly_3d)(double, constants));
int main() {
constants cst;
cst.A = 2;
cst.B = -8;
cst.C = 10;
printf("%f", NR_method(3.2, cst, poly_3d));
system("PAUSE");
return 0;
}
double poly_3d(double x, constants cst) {
double y = 3 * cst.A*x*x + 2 * cst.B*x + cst.C;
return y;
}
double NR_method(double a, constants cst, double (*poly)(double, constants)) {
return (*poly)(a, cst);
}
Let's start by simplifying your code. (A minimal example removes distractions, allowing you to better focus on the actual issue.) It looks like you started to do this, but it can be taken further. After removing some stuff that is not needed to reproduce the compile error:
class constants {};
double poly_3d(double x, constants cst);
double NR_method(double a, double(*poly_3d)(double));
int main() {
NR_method(3.2, poly_3d);
}
double poly_3d(double x, constants /*cst*/) {
return 3 * x;
}
double NR_method(double a, double (*poly_3d)(double)) {
return (*poly_3d)(a);
}
Now let's look at the error message:
error: invalid conversion from 'double (*)(double, constants)' to 'double (*)(double)'
This comes with an indication that the conversion is from poly_3d to the second argument of NR_method. If you look at those things, yes, that is the conversion you requested. The argument list for poly_3d is (double, constant), while the declared argument list for the second argument is just (double). There is a mismatch, which makes the conversion invalid. It's not all that different from the single-parameter case: the signatures must match. You can solve this by changing the argument's signature to math that of poly_3d.
Now, if you just make the signatures match, there is another problem in that NR_method does not have a constants value available. That is probably a logical error for you to work out. For a quick workaround to show the elimination of the compiler error, I'll add a local variable.
class constants {
};
double poly_3d(double x, constants cst);
double NR_method(double a, double(*poly_3d)(double, constants)); // <-- Desired signature
int main() {
NR_method(3.2, poly_3d);
}
double poly_3d(double x, constants /*cst*/) {
return 3.0 * x;
}
double NR_method(double a, double (*poly_3d)(double, constants)) {
constants cst; // <-- Allows this to compile, but probably not what you want.
return (*poly_3d)(a, cst); // <-- Needed a second parameter here.
}
There are ways to make this work nicer (for example, a std::function may be more convenient than a function pointer), but explaining those would fall outside the scope of this question, especially since some decisions would depend on the bigger picture.

C++ Defining a virtual base class overloaded operator

I am trying to write a set of generic math utility classes (root finders, integrators, etc.) that take in upon construction a pointer to a base class that defines the function I want the specific algorithm to operate on. The base class should only define a public virtual interface (abstract or with default trivial functionality) type operator()(type inputArg) that can be implemented by the user as needed. This would allow the user to just implement the needed functors and perform the needed computations. My mwe is below:
This first header defines the abstract interface class
// BaseFunctor.h
#ifndef _BASE_FUNCTOR_H_
#define _BASE_FUNCTOR_H_
class BaseFunctor
{
public:
virtual double operator() (double x) = 0;
};
#endif
This is the class for one of the solver methods
// NewtonsMethod.h
#ifndef _NEWTONS_METHOD_H_
#define _NEWTONS_METHOD_H_
class BaseFunctor;
class NewtonsMethod
{
public:
NewtonsMethod(BaseFunctor *rhsIn,
BaseFunctor *rhsPrimeIn,
double x0In);
~NewtonsMethod();
bool DetermineRoot(double &root);
private:
double x0;
BaseFunctor *rhs;
BaseFunctor *rhsPrime;
static const double EPSILON;
static const unsigned int MAX_ITER;
};
#endif
This is the solver implementation:
// NewtonsMethod.cpp
#include "NewtonsMethod.h"
#include "BaseFunctor.h"
#include <cmath>
const double NewtonsMethod::EPSILON = 1e-9;
const unsigned int NewtonsMethod::MAX_ITER = 30;
NewtonsMethod::NewtonsMethod(BaseFunctor *rhsIn,
BaseFunctor *rhsPrimeIn,
double x0In) :
rhs (rhsIn),
rhsPrime(rhsPrimeIn),
x0 (x0In)
{ }
NewtonsMethod::~NewtonsMethod() { }
bool NewtonsMethod::DetermineRoot(double &root)
{
// This is obviously not implemented
root = rhs(1.0) / rhsPrime(2.0);
return false;
}
And the main function where I make the derived class implementations:
// Main.cpp
#include "BaseFunctor.h"
#include "NewtonsMethod.h"
#include <iostream>
#include <iomanip>
class fOfX : public BaseFunctor
{
double operator() (double x)
{
return x * x - 2.0;
}
};
class fPrimeOfX : public BaseFunctor
{
double operator() (double x)
{
return 2.0 * x;
}
};
int main()
{
double x0 = 2.0;
BaseFunctor *f = new fOfX();
BaseFunctor *fPrime = new fPrimeOfX();
NewtonsMethod newton(f, fPrime, x0);
double root = 0.0;
bool converged = newton.DetermineRoot(root);
if (converged)
{
std::cout << "SUCCESS: root == " << std::setprecision(16) << root << std::endl;
}
else
{
std::cout << "FAILED: root == " << std::setprecision(16) << root << std::endl;
}
delete f;
delete fPrime;
}
I tried to make that as brief as possible, so sorry if it is too long. Basically I get the error:
g++ Main.cpp NewtonsMethod.cpp -o main
NewtonsMethod.cpp: In member function ‘bool NewtonsMethod::DetermineRoot(double&)’:
NewtonsMethod.cpp:29: error: ‘((NewtonsMethod*)this)->NewtonsMethod::rhs’ cannot be used as a function
NewtonsMethod.cpp:29: error: ‘((NewtonsMethod*)this)->NewtonsMethod::rhsPrime’ cannot be used as a function
How can I get this resolved keeping the desired functionality or deriving a class for the various needed functions?
Thanks
rhs and rhsPrime are pointers. You need to reference them in order for the function call operator to be invoked.
(*rhs)(1.0) / (*rhsPrime)(2.0)
If rhs and rhsPrime are required (i.e. cannot be NULL) and cannot be changed after the NewtonsMethod object has constructor you should declare them as references instead of pointers. This would also eliminate the need to dereference them to invoke the function call operator.
The example below shows how to use references to reference the functors.
class NewtonsMethod
{
public:
NewtonsMethod(BaseFunctor& rhsIn,
BaseFunctor& rhsPrimeIn,
double x0In);
~NewtonsMethod();
bool DetermineRoot(double &root);
private:
double x0;
BaseFunctor& rhs;
BaseFunctor& rhsPrime;
static const double EPSILON;
static const unsigned int MAX_ITER;
};
int main()
{
double x0 = 2.0;
fOfX f;
fPrimeOfX fPrime;
NewtonsMethod newton(f, fPrime, x0);
}
...or...
int main()
{
double x0 = 2.0;
BaseFunctor *f = new fOfX();
BaseFunctor *fPrime = new fPrimeOfX();
NewtonsMethod newton(*f, *fPrime, x0);
// ... other code including delete for the functors
}

C++ member function using another member function

I'm facing a big issue which I have been trying to solve in vain for 3 days. I've got a CDS class with a intensity_func member function and a big_gamma member function which is basically the integral of the member intensity_func function.
#include <vector>
#include <cmath>
using namespace std
class CDS
{
public:
CDS();
CDS(double notional, vector<double> pay_times, vector<double> intensity);
~CDS();
double m_notional;
vector<double> m_paytimes;
vector<double> m_intensity;
double intensity_func(double);
double big_gamma(double);
};
And here is the CDS.cpp with the definition of the intensity_func member function :
#include <vector>
#include <random>
#include <cmath>
#include "CDS.h"
double CDS::intensity_func(double t)
{
vector<double> x = this->m_intensity;
vector<double> y = this->m_paytimes;
if(t >= y.back() || t< y.front())
{
return 0;
}
else
{
int d=index_beta(y, t) - 1;
double result = x.at(d) + (x.at(d+1) - x.at(d))*(t - y.at(d))/ (y.at(d+1) - y.at(d));
return result;
}
I have implemented in another source file a function to integrate function and the index_beta function used in the intensity_func member function (using the Simpson's rule). Here is the code:
double simple_integration ( double (*fct)(double),double a, double b)
{
//Compute the integral of a (continuous) function on [a;b]
//Simpson's rule is used
return (b-a)*(fct(a)+fct(b)+4*fct((a+b)/2))/6;
};
double integration(double (*fct)(double),double a, double b, double N)
{
//The integral is computed using the simple_integration function
double sum = 0;
double h = (b-a)/N;
for(double x = a; x<b ; x = x+h) {
sum += simple_integration(fct,x,x+h);
}
return sum;
};
int index_beta(vector<double> x, double tau)
{
// The vector x is sorted in increasing order and tau is a double
if(tau < x.back())
{
vector<double>::iterator it = x.begin();
int n=0;
while (*it < tau)
{
++ it;
++n; // or n++;
}
return n;
}
else
{
return x.size();
}
};
So, what I would like to have in my CDS.cpp to define the big_gamma member function is :
double CDS::big_gamma(double t)
{
return integration(this->intensity, 0, t);
};
But obviously, it does not work and I get the following error message : reference to non static member function must be called. I've then tried to turn the intensity member function into a static function but new problems come out: I can't used this->m_intensity and this->m_paytimes anymore as I get the following error message: Invalid use of this outside a non-static member function.
double (*fct)(double) declares an argument of type "pointer-to-function". You need to declare that as a "pointer-to-member function" instead: double (CDS::*fct)(double). Furthermore, you need an object on which you call the pointer-to-member:
(someObject->*fct)(someDouble);

Storing function pointers

The following uses a simple function pointer, but what if I want to store that function pointer? In that case, what would the variable declaration look like?
#include <iostream>
#include <vector>
using namespace std;
double operation(double (*functocall)(double), double wsum);
double get_unipolar(double);
double get_bipolar(double);
int main()
{
double k = operation(get_bipolar, 2); // how to store get_bipolar?
cout << k;
return 0;
}
double operation(double (*functocall)(double), double wsum)
{
double g = (*functocall)(wsum);
return g;
}
double get_unipolar(double wsum)
{
double threshold = 3;
if (wsum > threshold)
return threshold;
else
return threshold;
}
double get_bipolar(double wsum)
{
double threshold = 4;
if (wsum > threshold)
return threshold;
else
return threshold;
}
You code is almost done already, you just seem to call it improperly, it should be simply
double operation(double (*functocall)(double), double wsum)
{
double g;
g = functocall(wsum);
return g;
}
If you want to have a variable, it's declared in the same way
double (*functocall2)(double) = get_bipolar;
or when already declared
functocall2 = get_bipolar;
gives you a variable called functocall2 which is referencing get_bipolar, calling it by simply doing
functocall2(mydouble);
or passing it to operation by
operation(functocall2, wsum);
You already (almost) have it in your code:
double (*functocall)(double) = &get_bipolar;
This defines a function pointer named functocall which points to get_bipolar.
typedef double (*func_t)(double);
func_t to_be_used = get_bipolar
typedef double (*PtrFunc)(double);
PtrFunc ptrBipolar = get_bipolar;
OR
typedef double (Func)(double);
Func *ptrBipolar = get_bipolar;
which ever you are comfortable to use.
Have a look at boost function, it's a header only library that tidies things up a little (IMHO):
http://www.boost.org/doc/libs/1_42_0/doc/html/function.html
typedef boost::function<double (double)> func_t;
func_t to_be_used = &get_bipolar;
(NB: different syntax required for VC6)
double (*foo)(double);
where foo is the variable name.
You should consider using a typedef:
typedef double (*MyFunc)(double);
MyFunc ptr_func = &get_bipolar;
(*ptr_func)(0.0);
double operation(MyFunc functocall, double wsum)
{
double g;
g = (*functocall)(wsum);
return g;
}
May I recommend also the identity template trick:
template<class T>
class Id
{
typedef T type;
};
Id<double(double)>::type * ptr_func = &get_bipolar;
MyFunc func = &get_bipolar;
(*ptr_func)(0.0);
double operation(Id<double(double)>::type * functocall, double wsum)
{
double g;
g = (*functocall)(wsum);
return g;
}