Function pointer problem

Hi,

I have a class that is defined by this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <PayOff1_Ex21.h>
#include <MinMax.h>

PayOff::PayOff(double Strike_, double (*f_)(double ,double ))
{
	Strike = Strike_;
	FunctionPtr = f_;
}

double PayOff::operator ()(double spot) const
{
	return FunctionPtr(Strike , spot);

}




and I have an argument problem from the file below:
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
#include<SimpleMC_Ex21.h>
#include<iostream>
using namespace std;

double digitaloptions(double , double );

int main()
{

	double Expiry;
	double Strike; 
	double Spot; 
	double Vol; 
	double r; 
	unsigned long NumberOfPaths;

	cout << "\nEnter expiry\n";
	cin >> Expiry;

	cout << "\nEnter strike\n";
	cin >> Strike;

	cout << "\nEnter spot\n";
	cin >> Spot;

	cout << "\nEnter vol\n";
	cin >> Vol;

	cout << "\nr\n";
	cin >> r;

	cout << "\nNumber of paths\n";
	cin >> NumberOfPaths;

    PayOff callPayOff(Strike, digitaloptions);
    PayOff putPayOff(Strike, digitaloptions);

	double resultCall = SimpleMonteCarlo2(callPayOff,
                                          Expiry,                                           
							              Spot, 
							              Vol, 
							              r, 
						                  NumberOfPaths);
	
    double resultPut = SimpleMonteCarlo2(putPayOff,
                                         Expiry,                                           
							             Spot, 
							             Vol, 
							             r, 
						                 NumberOfPaths);

	cout <<"the prices are " << resultCall << "  for the call and " 
                                    << resultPut << " for the put\n";

    double tmp;
    cin >> tmp;

	return 0;

}

double digitaloptions(double Spot_d, double Strike_d)
{
	//return max(Spot_d-Strike_d,0);

	return 0;

}



Also, what is wrong with the followings lines :
1
2
PayOff callPayOff(Strike, digitaloptions);
PayOff putPayOff(Strike, digitaloptions);


The errror messages i presently have is :

1>SimpleMCMain2_Ex21.cpp
1>c:\moondragon\distancelearning\cpp\code\chap2.1\simplemcmain2_ex21.cpp(35) : error C2664: 'PayOff::PayOff(double,double (__cdecl *)(double,double))' : cannot convert parameter 2 from 'double' to 'double (__cdecl *)(double,double)'
1> There is no context in which this conversion is possible
1>c:\moondragon\distancelearning\cpp\code\chap2.1\simplemcmain2_ex21.cpp(36) : error C2664: 'PayOff::PayOff(double,double (__cdecl *)(double,double))' : cannot convert parameter 2 from 'double' to 'double (__cdecl *)(double,double)'
1> There is no context in which this conversion is possible
1>Build log was saved at "file://c:\MoonDragon\DistanceLearning\cpp\code\chap2.1\Debug\BuildLog.htm"
1>chap2.1 - 2 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========



Thanks and regards
Are you absolutely sure you have this:
1
2
    PayOff callPayOff(Strike, digitaloptions);
    PayOff putPayOff(Strike, digitaloptions);


and not something like this (which would produce the type of error you reported)
1
2
    PayOff callPayOff(Strike, digitaloptions());
    PayOff putPayOff(Strike, digitaloptions());
Hello,

Definitively sure about this.

What i have is :

1
2
    PayOff callPayOff(Strike, digitaloptions);
    PayOff putPayOff(Strike, digitaloptions);
You could post the full code - I'm intrigued.

(and you could always try a clean followed by a rebuild)
PayOff1_Ex21.cpp :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <PayOff1_Ex21.h>
#include <MinMax.h>

PayOff::PayOff(double Strike_, double (*f_)(double ,double ))
{
	Strike = Strike_;
	FunctionPtr = f_;
}

double PayOff::operator ()(double spot) const
{
	return FunctionPtr(Strike , spot);

}


PayOff1_Ex21.h :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//                               PayOff1.h

#ifndef PAYOFF_H
#define PAYOFF_H

class PayOff
{
private:
    double Strike;
	double (*FunctionPtr)(double , double);

public:

    PayOff(double Strike_, double (*f_) (double,double));
    double operator()(double Spot) const;

};

#endif 



SimpleMC_Ex21.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
//                      SimpleMC.cpp
//
//

#include<SimpleMC_Ex21.h>
#include <Random1.h>
#include <cmath>

// the basic math functions should be in namespace std but aren't in VCPP6
#if !defined(_MSC_VER)
using namespace std;
#endif

double SimpleMonteCarlo2(const PayOff& thePayOff,
                         double Expiry,  
						 double Spot, 
						 double Vol, 
						 double r, 
						 double strike,
						 unsigned long NumberOfPaths)
{

	double variance = Vol*Vol*Expiry;
	double rootVariance = sqrt(variance);
	double itoCorrection = -0.5*variance;

	double movedSpot = Spot*exp(r*Expiry +itoCorrection);
	double thisSpot;
	double runningSum=0;

	for (unsigned long i=0; i < NumberOfPaths; i++)
	{
		double thisGaussian = GetOneGaussianByBoxMuller();
		thisSpot = movedSpot*exp( rootVariance*thisGaussian);
		double thisPayOff = thePayOff(thisSpot);
		runningSum += thisPayOff;
	}

	double mean = runningSum / NumberOfPaths;
	mean *= exp(-r*Expiry);
	return mean;
}
Last edited on
Random1.h (but i don't think the problems come from here):

1
2
3
4
5
6
7
8
9
10
11
12
//			               	Random1.h
//
//             


#ifndef RANDOM1_H
#define RANDOM1_H

double GetOneGaussianBySummation();
double GetOneGaussianByBoxMuller();

#endif 


Random.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
//       				Random1.cpp
//
//           

#include <Random1.h>
#include <cstdlib>
#include <cmath>

// the basic math functions should be in namespace std but aren't in VCPP6
#if !defined(_MSC_VER)
using namespace std;
#endif

double GetOneGaussianBySummation()
{
	double result=0;

	for (unsigned long j=0; j < 12; j++)
		result += rand()/static_cast<double>(RAND_MAX);

	result -= 6.0;

	return result;

}


double GetOneGaussianByBoxMuller()
{
	double result;

	double x;
	double y;

	double sizeSquared;
	do
	{
		x = 2.0*rand()/static_cast<double>(RAND_MAX)-1;
		y = 2.0*rand()/static_cast<double>(RAND_MAX)-1;
		sizeSquared = x*x + y*y;
	}
	while
		( sizeSquared >= 1.0);

	result = x*sqrt(-2*log(sizeSquared)/sizeSquared);

	return result;

}
What about main.cpp?
(I don't see anything in the files you hve posted so far...)
Last edited on
The main is SimpleMCMain2_Ex21.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
#include<SimpleMC_Ex21.h>
#include<iostream>
using namespace std;

double digitaloptions(double , double );

int main()
{

	double Expiry;
	double Strike; 
	double Spot; 
	double Vol; 
	double r; 
	unsigned long NumberOfPaths;

	cout << "\nEnter expiry\n";
	cin >> Expiry;

	cout << "\nEnter strike\n";
	cin >> Strike;

	cout << "\nEnter spot\n";
	cin >> Spot;

	cout << "\nEnter vol\n";
	cin >> Vol;

	cout << "\nr\n";
	cin >> r;

	cout << "\nNumber of paths\n";
	cin >> NumberOfPaths;

    PayOff callPayOff(Strike, digitaloptions);
    PayOff putPayOff(Strike, digitaloptions);

	double resultCall = SimpleMonteCarlo2(callPayOff,
                                          Expiry,                                           
							              Spot, 
							              Vol, 
							              r, 
						                  NumberOfPaths);
	
    double resultPut = SimpleMonteCarlo2(putPayOff,
                                         Expiry,                                           
							             Spot, 
							             Vol, 
							             r, 
						                 NumberOfPaths);

	cout <<"the prices are " << resultCall << "  for the call and " 
                                    << resultPut << " for the put\n";

    double tmp;
    cin >> tmp;

	return 0;

}

double digitaloptions(double Spot_d, double Strike_d)
{
	//return max(Spot_d-Strike_d,0);

	return 0;

}
OK - I give up - it seems ok to me .
The only thing(s) I can suggest is to do a clean/rebuild - or make a new project.
You need to pass the function "digitaloptions()" by reference. So the call to your constructor should look more like this: PayOff callPayOff(Strike, &digitaloptions);.
I have tried it but i still having the same error message.
I'll have to agree with guestgulkan then, do a clean rebuild. This happens to everyone from time to time and it's a little annoying because it's almost impossible to spot but it might fix this issue.
I have started a new project but still having the same error....
Hello,

The problem came from the declaration of SimpleMonteCarlo2 : its signature was different from the one in the .h file.
Topic archived. No new replies allowed.