unresolved external symbol

I am not entirely sure why i got these errors:

1
2
3
main.obj : error LNK2019: unresolved external symbol "public: __thiscall BlackScholes::BlackScholes(double,double,double,double,double,float)" (??0BlackScholes@@QAE@NNNNNM@Z) referenced in function _main
main.obj : error LNK2019: unresolved external symbol "public: double __thiscall BlackScholes::derive_c(void)" (?derive_c@BlackScholes@@QAENXZ) referenced in function _main
main.obj : error LNK2019: unresolved external symbol "public: double __thiscall BlackScholes::derive_p(void)" (?derive_p@BlackScholes@@QAENXZ) referenced in function _main


main.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
40
41
42
43
44
45
46
#include <iostream>

#include "BlackScholes.h"

int main() {

    std::cout << "Question 1 \n\n";
    // Q 1.1
    BlackScholes bs1 {50.0, 50.0, 0.0, 0.5, 0.2, 0.01};
    double call_value {};
    double put_value {}; 
    call_value = bs1.derive_c();
    put_value = bs1.derive_p();
    std::cout << "Q1.1 " << "Call Option Value: " << call_value << std::endl;
    std::cout << "Q1.1 " << "Put Option Value: " << put_value << std::endl;

    // Q 1.2
    BlackScholes bs2 {50.0, 60.0, 0.0, 0.5, 0.2, 0.01};
    call_value = bs2.derive_c();
    put_value = bs2.derive_p();
    std::cout << "Q1.2 " << "Call Option Value: " << call_value << std::endl;
    std::cout << "Q1.2 " << "Put Option Value: " << put_value << std::endl;

    // Q 1.3
    BlackScholes bs3 {50.0, 50.0, 0.0, 1.0, 0.2, 0.01};
    call_value = bs3.derive_c();
    put_value = bs3.derive_p();
    std::cout << "Q1.3 " << "Call Option Value: " << call_value << std::endl;
    std::cout << "Q1.3 " << "Put Option Value: " << put_value << std::endl;

    // Q 1.4
    BlackScholes bs4 {50.0, 50.0, 0.0, 0.5, 0.3, 0.01};
    call_value = bs4.derive_c();
    put_value = bs4.derive_p();
    std::cout << "Q1.4 " << "Call Option Value: " << call_value << std::endl;
    std::cout << "Q1.4 " << "Put Option Value: " << put_value << std::endl;

    // Q 1.5
    BlackScholes bs5 {50.0, 50.0, 0.0, 0.5, 0.2, 0.02};
    call_value = bs5.derive_c();
    put_value = bs5.derive_p();
    std::cout << "Q1.5 " << "Call Option Value: " << call_value << std::endl;
    std::cout << "Q1.5 " << "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
#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
#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);
}
As 1 file, it compiles OK. What compiler are you using? Have you told the compiler to also compile BlackScholes.cpp as well as main.cpp ?
This works fine for me in VS 2019, here are a few resources that might help you out.

The Cherno: How the C++ Linker Works
https://www.youtube.com/watch?v=H4s55GgAg0I

LearnCPP.com: External Linkage
https://www.learncpp.com/cpp-tutorial/external-linkage/

Edit: seeplus is correct, you might not have your Blackscholes.cpp file added to your project, because in VS 2019 I removed it from the project and got these errors:


Error	LNK2019	unresolved external symbol "public: __thiscall BlackScholes::BlackScholes(double,double,double,double,double,float)" (??0BlackScholes@@QAE@NNNNNM@Z) referenced in function _main
Error	LNK1120	3 unresolved externals
Error	LNK2019	unresolved external symbol "public: double __thiscall BlackScholes::derive_c(void)" (?derive_c@BlackScholes@@QAENXZ) referenced in function _main	
Error	LNK2019	unresolved external symbol "public: double __thiscall BlackScholes::derive_p(void)" (?derive_p@BlackScholes@@QAENXZ) referenced in function _man


Which is the errors you're getting. What compiler are you using?
Last edited on
You guys are completely correct. It was the linker issue. I was using VS Code and I am still quite new to using it for C++. I used to use CodeLite and now I use Visual Studio and it works~~

I will further figure out how to simply use VS Code and workout the linker stuff~~ Thanks!!
Topic archived. No new replies allowed.