0

I'm working on some quant-finance related coding in VS C++, and have been getting errors with .h and .cpp files. I noticed the issues go away when I do everything within .cpp files, but still would like to understand why I am seeing errors when taking a split approach.

When running the below "ConsoleApplication1.cpp" file I get a number of errors of the form:

Severity Code Description Project File Line Suppression State Error LNK2005 "public: __thiscall GBM1D::GBM1D(double,double)" (??0GBM1D@@QAE@NN@Z) already defined in ConsoleApplication1.obj ConsoleApplication1 ... 1

Any help would be greatly appreciated!!

Thanks

"ConsoleApplication1.cpp"

#include <iostream>
#include "GBM1D.h"
#include "GBM1D.cpp"




int main()
{


    double mu = 0.02;
    double sigma = 0.3;

    GBM1D(mu,sigma);

    return 0;

};

"GBM1D.cpp"

#include "GBM1D.h"

GBM1D::GBM1D()
{
    drift = 0.0;
    vol = 0.0;
}

GBM1D::GBM1D(double drift_, double vol_)
{

    drift = drift_;
    vol  = vol_;

};

double GBM1D::mu(double S)
{
    return drift * S;
};

double GBM1D::sigma(double S)
{
    return vol * S;
}

"GBM1D.h"

#pragma once
class GBM1D 
{

public:
    
    
    GBM1D();
    GBM1D(double drift_, double vol_);
    
    double mu(double S);
    double sigma(double S);
    

private:

    double drift;
    double vol;

};
d_797
  • 101
  • 2

1 Answers1

1

You shouldn't include both .h and .cpp do and you should construct your GBM1D

On your ConsoleApplication1.cpp change

#include "GBM1D.h"
#include "GBM1D.cpp"

To

#include "GBM1D.h"

And in your main change

double mu = 0.02;
double sigma = 0.3;

GBM1D(mu,sigma);

To

double mu = 0.02;
double sigma = 0.3;

GBM1D yourChoice(mu, sigma);
Emre Erdog
  • 23
  • 1
  • 5