Undefined reference

Hi,

I am doing this coding and I don't know why it came out as "undefined reference"
This is the code

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "Solver03.h"
#include "Margrabe.h"
#include "EurCall.h"
#include <iostream>
#include <fstream>
#include <cmath>


using namespace std;

class Intermediary: public EurCall
{
   private:
      double S,r,q;
   public:
      Intermediary(double S_, double r_,double q_, double T_, double K_)
         : EurCall(T_,K_) {S=S_; r=r_;q=q_;}
     double Value(double sigma)
      {
         return PriceByBSFormula(S, sigma, r, q);
      }
};

class Intermediary1 : public Margrabe
{
     private :
        double S10, S20, q10, q20, sigma10, sigma20;
     public :
        Intermediary1(double S10_, double S20_, double q10_, double q20_, double sigma10_, double sigma20_, double T_)
        : Margrabe (T_){S10=S10_; S20=S20_; q10=q10_; q20=q20_; sigma10=sigma10_; sigma20=sigma20_;}
        double Value(double rho)
        {
            return PriceByMargrabeFormula(S10, S20, q10, q20, sigma10, sigma20, rho);
        }
};

int main()
{
    double r=0.06;
    double S1 = 95;
    double S2 = 105;
    double K1 = 95;
    double K2 = 105;
    double q1 = 0.03;
    double q2 = 0.05;
    double T1 = 0.5;
    double T2 = 0.8;
    double T3  = 0.6;
    double sigma1 = 0.28;
    double sigma2 = 0.009;

   Intermediary Call_1(S1,q1,r,T1,K1);
   Intermediary Call_2(S2,q2,r,T2,K2);
   Intermediary1 Margrabe1 (S1, S2, T3,q1, q2, sigma1, sigma2);

   double Acc=0.001;
   double LEnd=0.01, REnd=1.0;
   double Tgt_1=8.1206;
   double Tgt_2= 5.2856;
   double Tgt_3= 6.8833;


   cout << "First Implied volatility by bisect: "
        << SolveByBisect(&Call_1,Tgt_1,LEnd,REnd,Acc) << '\n'
        << "Second Implied volatility by bisect: "
        << SolveByBisect(&Call_2,Tgt_2,LEnd,REnd,Acc) << '\n'
      << "Imlied Correlation Coefficient: "
    << SolveByBisect(&Margrabe1, Tgt_3, LEnd, REnd, Acc)
        << endl;

   return 0;
}


solver03.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#ifndef Solver03_h
#define Solver03_h

template<typename Function> double SolveByBisect
   (Function* Fct,
   double Tgt, double LEnd, double REnd, double Acc)
{
   double left=LEnd, right=REnd, mid=(left+right)/2;
   double y_left=Fct->Value(left)-Tgt, y_mid=Fct->Value(mid)-Tgt;
   while (mid-left>Acc)
   {
      if ((y_left>0 && y_mid>0)||(y_left<0 && y_mid<0))
         {left=mid; y_left=y_mid;}
      else right=mid;
      mid=(left+right)/2;
      y_mid=Fct->Value(mid)-Tgt;
   }
   return mid;
}


#endif 


margrabe.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef MARGRABE_H_INCLUDED
#define MARGRABE_H_INCLUDED



class Margrabe
{
   public:
      double T;
     Margrabe(double T_){T=T_;}
      double d_plus1(double S10, double S20, double q10, double q20, double sigma10, double sigma20, double rho);
      double d_minus1(double S10, double S20, double q10, double q20, double sigma10, double sigma20, double rho);
      double sigma (double sigma10, double sigma20, double rho);
      double PriceByMargrabeFormula(double S10, double S20, double q10, double q20, double sigma10, double sigma20,
									 double rho);
};


#endif // MARGRABE_H_INCLUDED 


margrabe.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 "Margrabe.h"
#include <cmath>

double N(double x)
{
   double gamma = 0.2316419;     double a1 = 0.319381530;
   double a2    =-0.356563782;   double a3 = 1.781477937;
   double a4    =-1.821255978;   double a5 = 1.330274429;
   double pi    = 4.0*atan(1.0); double k  = 1.0/(1.0+gamma*x);
   if (x>=0.0)
   {
      return 1.0-((((a5*k+a4)*k+a3)*k+a2)*k+a1)
                  *k*exp(-x*x/2.0)/sqrt(2.0*pi);
   }
   else return 1.0-N(-x);
}

 //for implied correlation coefficient

double Margrabe:: sigma (double sigma10, double sigma20, double rho);
{
    return sqrt(pow(sigma10,2) + pow(sigma20,2) - 2* rho*sigma10*sigma20);
}
double Margrabe::d_plus1(double S10, double S20, double q10, double q20, double sigma10, double sigma20, double rho);
{
    return (log(S10/S20)+(q20 - q10 + 0.5*(pow(sigma(sigma10, sigma20, rho),2)))*T)/(sigma*T);
}

double Margrabet::d_minus1(double S10, double S20, double q10, double q20, double sigma10, double sigma20, double rho))
{
    return (log(S10/S20)+(q20 - q10 - 0.5*(pow(sigma(sigma10, sigma20, rho),2)))*T)/(sigma*T);
}

double Margrabe:: PriceByMargrabeFormula(double S10, double S20, double q10, double q20, double sigma10, double sigma20,
									 double rho)
{
    return exp(-q10*T)*S10*N(d_plus1(S10, S20, q10, q20, sigma10, sigma20, rho) -
    exp(-q20*T)*S20*N(d_minus1(S10, S20, q10, q20, sigma10, sigma20, rho));
}



EurCall.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef EurCall_h
#define EurCall_h

class EurCall
{
   public:
      double T, K;
      EurCall(double T_, double K_){T=T_; K=K_;}
      double d_plus(double S, double sigma, double r, double q);
      double d_minus(double S, double sigma, double r, double q);
      double PriceByBSFormula(double S, double sigma, double r, double q);
};

#endif 


EurCall.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
#include "EurCall.h"
#include <cmath>

double N(double x)
{
   double gamma = 0.2316419;     double a1 = 0.319381530;
   double a2    =-0.356563782;   double a3 = 1.781477937;
   double a4    =-1.821255978;   double a5 = 1.330274429;
   double pi    = 4.0*atan(1.0); double k  = 1.0/(1.0+gamma*x);
   if (x>=0.0)
   {
      return 1.0-((((a5*k+a4)*k+a3)*k+a2)*k+a1)
                  *k*exp(-x*x/2.0)/sqrt(2.0*pi);
   }
   else return 1.0-N(-x);
}

double EurCall::d_plus(double S, double sigma, double r, double q)
{
   return (log(S/K)+(r-q+(0.5*pow(sigma,2.0))*T)/(sigma*sqrt(T)));
}

double EurCall::d_minus(double S, double sigma, double r, double q)
{
   return (log(S/K)+(r-q-(0.5*pow(sigma,2.0))*T) /(sigma*sqrt(T)));
}

double EurCall::PriceByBSFormula(double S, double sigma, double r, double q)
{
   return S*N(d_plus(S,sigma,r,q)) - K*exp(-r*T)*N(d_minus(S,sigma,r,q));
}


Please somebody help me.

The error came out like this

||=== Build: Debug in project2 (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function `ZN13Intermediary15ValueEd':|
E:\Project2\main.cpp|32|undefined reference to `Margrabe::PriceByMargrabeFormula(double, double, double, double, double, double, double)'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
Last edited on
Please edit your post and make sure each of your code files is [code]between code tags[/code] so that they have line numbers and proper indentation, as well as syntax highlighting.

Please copy and paste the exact, full error message(s).
Last edited on
¿what is your build command?
Your project is setup incorrectly, you couldn't reach the linking stage because "margrabe.cpp" does not compile.

To understand the error message http://www.cplusplus.com/forum/general/113904/


By the way, when you have several files try to upload to github or similar so we don't have to bother to replicate your file structure.
Topic archived. No new replies allowed.