identifier is not defined and header file issues
In my main.cpp, even with #include "Blackscholes.h", there is still errors due to identifier undefined. I am not sure what went wrong. :(
main.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
#include <iostream>
#include "Blackscholes.h"
int main() {
Blackscholes bs;
std::cout << "Question 1 \n\n";
// Q 1.1
Blackscholes bs {50.0, 50.0, 0.0, 0.5, 0.2, 0.01};
double call_value {};
double put_value {};
call_value = bs.derive_c();
put_value = bs.derive_p();
std::cout << "Q1.1 " << "Call Option Value: " << call_value << std::endl;
std::cout << "Q1.1 " << "Put Option Value: " << put_value << std::endl;
return 0;
}
|
Blackscholes.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#ifndef _BLACKSCHOLES_H_
#define _BLACKSCHOLES_H_
#include <iostream>
#include <cmath>
class BlackScholes {
private:
double S; // stock price
double K; // strike price
float r; // risk free rate
double sigma;
double T;
double t;
// to be calculated
double d1;
double d2;
public:
BlackScholes(double s_val, double k_val, double t_val, double T_val, double sigma_val, float r_val);
// function to calculate d1
double derive_d1();
// function to calculate d2
double derive_d2();
// function to calculate call option
double derive_c();
// function to calculate put option
double derive_p();
void display_c();
void display_p();
};
#endif
|
Blackscholes.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
#include "Blackscholes.h"
BlackScholes::BlackScholes(double s_val, double k_val, double t_val, double T_val, double sigma_val, float r_val)
: S{s_val}, K{k_val}, t{t_val}, T{T_val}, sigma{sigma_val}, r{r_val} {
}
// function to calculate d1
double BlackScholes::derive_d1() {
return (log(S/K) + r*(T-t))/(sigma * sqrt(T-t)) + 0.5 * sigma * sqrt (T-t);
}
// function to calculate d2
double BlackScholes::derive_d2() {
return (log(S/K) + r*(T-t))/(sigma * sqrt(T-t)) - 0.5 * sigma * sqrt (T-t);
}
// function to calculate call option
double BlackScholes::derive_c() {
d1 = BlackScholes::derive_d1();
d2 = BlackScholes::derive_d2();
return S * d1 - K * exp(-r * (T-t)) * d2;
}
// function to calculate put option
double BlackScholes::derive_p() {
d1 = BlackScholes::derive_d1();
d2 = BlackScholes::derive_d2();
return K * exp(-r * (T-t)) * (-d2) - S*(-d1);
}
void BlackScholes::display_c(){
std::cout << BlackScholes::derive_c();
}
void BlackScholes::display_p(){
std::cout << BlackScholes::derive_p();
}
|
Last edited on
> Blackscholes bs;
CaSe MattERS
> class BlackScholes
Also, the first 'bs' on line 7 is useless as you declare it properly a few lines later.
Last edited on
Topic archived. No new replies allowed.