0

This is my code, the exact error is at the bottom, I've looked throughout stack overflow and can't find an answer to my question. I have no idea why I'm getting this error

//amer_bi.h

    #include <iostream>
    using namespace std;



    class amer_bi
    {
    public:
        int steps,i,j;
        double risk_free, price, strike, ttm, u, d, p, vol, disc, dt;
        char sw;
        double OptionPrice(double, double, double, double, double);
        double max(double , double);
    };

//amer_bi.cpp
#include "amer_bi.h"
#include <iostream>
#include <math.h>
#include <fstream>
#include <string>
#include <stdio.h>
using namespace std;
#include <string>
//max function create
double max( double d1, double d2 )
{
   return ( d1 > d2 ) ? d1 : d2;
}

double OptionPrice(double risk_free, double price, double strike, double ttm,  double vol)
{

int steps;
steps = 200;

int i;
int j;

const int rows = steps+1;
const int cols = steps+1;

double dt = ttm/steps;
double u = exp(vol*sqrt(dt));
double d = exp(-vol*sqrt(dt));
double p = .5 + ((risk_free - .5*vol*vol)/(2*vol))*sqrt(dt);
double disc = exp(-risk_free*dt);

//pointer code for multidimensional dynamic array
double **price_array; 
double **disc_array;
double **call_array;

price_array=new double*[rows]; 
disc_array=new double*[rows];
call_array=new double*[rows];

for(int i=0; i<rows; ++i)
{
    price_array[i]=new double[cols];
    disc_array[i]=new double[cols];
    call_array[i]=new double[cols];
}


/*
//test data for book example
u = 1.1;
d = .9091;
disc = .9802;
p = .5820;
*/

char sw = 'c';

disc_array[steps][steps] = price*pow(d,steps);
for (i=steps; i > 0; i--)
{
    disc_array[i-1][steps] = disc_array[i][steps]*u/d;
}

for (i=steps; i>=0; i--)
{
    for (j=steps-1; j>=0; j--)
    {
        disc_array[i][j] = disc_array[i][j+1]*d;
    }
}

for (i=steps; i >= 0; i--)
{
    if (sw == 'c')  
        call_array[i][steps] = max(disc_array[i][steps] - strike, 0);
    else
        call_array[i][steps] = max(strike - disc_array[i][steps], 0);
}

price_array[0][steps] = price*pow(d,steps);

for (i=steps-1; i >=0; i--)
{
    for (j=steps-1; j>=0; j--)
    {
        if (sw == 'c')
            call_array[i][j] = max(disc*(p*call_array[i][j+1] + (1-p)*call_array[i+1][j+1]), disc_array[i][j] - strike);
        else
            call_array[i][j] = max(disc*(p*call_array[i][j+1] + (1-p)*call_array[i+1][j+1]), strike - disc_array[i][j]);
    }
}

//std::cout << call_array[0][0] << endl;

return call_array[0][0];
}


//top.cpp
#include <iostream>
#include "amer_bi.h"
using namespace std;






int main ()
{

    amer_bi exa;



    exa.OptionPrice(.06,  100.0, 120.0, 1.0, .2);

    system("pause");

    return 0;

} 

error LNK2019: unresolved external symbol "public: double __thiscall amer_bi::OptionPrice(double,double,double,double,double)" (?OptionPrice@amer_bi@@QAENNNNNN@Z) referenced in function _main 1>C:\Users\Class2017\Documents\Visual Studio 2010\Projects\QF 465\Debug\amer_bi.exe : fatal error LNK1120:

  • You are declaring `OptionPrice` as a member function but defining it as a free function. that's not going to work out well. – Captain Obvlious Apr 27 '15 at 23:57
  • possible duplicate of [error LNK2019: unresolved external symbol \_main referenced in function \_\_\_tmainCRTStartup](http://stackoverflow.com/questions/4845410/error-lnk2019-unresolved-external-symbol-main-referenced-in-function-tmainc) – m0nhawk Apr 28 '15 at 08:17

1 Answers1

2

The Error Your Getting is Because you are not implementing the method amer_bi::Option_Price. You Are Implementing the function Option_Price but not the method. As A result the linker is failing to find the methods implementation and raising a lnk2019 error. to implement a method of a class you either need to put the implementation in the class(marking the method as inline) or when implementing it out side the class refer to the name as amer_bi::Option_Price so that it knows what method to implement. e.g.

//.hpp file

class foo{

public:

//method prototype
void method();

};

//.cpp file

//what you need to be doing
void foo::method(){
    //method implementation
}

//what you are actually doing
void method(){
    //function implementation
}