Pointers to Functions

I'm very new to c++ and use the book Introduction to C++ Programming and Graphics. Unfortunately it's riddled with typos. In particular I have a hard time figuring out what is wrong with the code below.

I think prat and prod should be double *, but I am not sure. I also get a expected '; ' after top level declarator error.


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
#include <iostream>
using namespace std;

double ratio(double, double);
double (*pt_ratio)(double, double)=ratio;

double product(double, double);
double (*pt_product)(double, double)=product;

double operate( double, double, double(*)(double, double) );


int main(int argc, const char * argv[] )
{
    int menu;
    int a = 1.2;
    int b = 2.3;
    double result;
    
    cout << "Please enter 1 for the ratio and 2 for the product" << endl;
    cout << "q to quit"<< endl;
    
    while(cin >> menu)
    {
        if( menu == 1 )
        {
            result = operate( a , b , prat);
            cout << a << "/" << b << "=" << result;
        }
        else if( menu == 2)
        {
            result = operate( a , b , pprod );
            cout << a << "x" << b << "=" << result;
        }
        
    }
    return 0;

}


double ratio(double a, double b)
{
    c=a/b;
    return c;
}

double product(double a, double b)
{
    c=a*b;
    return c;
}

double operate(double a, double b, double (*funcall)(double, double))
{
    double c=(*funcall)(a,b);
    return c;
}
You have few undeclared variables:
Line 27 (prat) and line 32 (pprod): did you mean pt_ratio and pt_product ?
Line 44 must be double c=a/b;, same goes for line 50
Line 16,17: variables a and b should be declared as doubles
In this code snip

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    while(cin >> menu)
    {
        if( menu == 1 )
        {
            result = operate( a , b , prat);
            cout << a << "/" << b << "=" << result;
        }
        else if( menu == 2)
        {
            result = operate( a , b , pprod );
            cout << a << "x" << b << "=" << result;
        }
        
    }


function operate is called. It has as the third parameter a pointer to a function that accepts two double arguments. As in the very beginning of your program two pointers to function are declared

double (*pt_ratio)(double, double)=ratio;
double (*pt_product)(double, double)=product;

then they shall be called. So instead of prat and pprod which are not declared pt_ration and pt_product shall be used as arguments of the operate.
Thanks! So many typos I made! But, hm. Ok, So I have this now. And get the error No matching function for call to 'operate' in line 31 and 36, and Expected '; ' after top level declarator at line


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
#include <iostream>
#include <fstream>
using namespace std;

double ratio(double, double);
double (*pt_ratio)(double, double)=ratio;

double product(double, double);
double (*pt_product)(double, double)=product;

double operate( double, double, double(*)(double, double) );


int main(int argc, const char * argv[] )
{
    
    int menu;
    double a = 1.2;
    double b = 2.3;
    double result;
    double * pt_ratio;
    double * pt_product;
    
    cout << "Please enter 1 for the ratio and 2 for the product" << endl;
    cout << "q to quit"<< endl;
    
    while(cin >> menu)
    {
        if( menu == 1 )
        {
            result = operate( a , b , pt_ratio);
            cout << a << "/" << b << "=" << result;
        }
        else if( menu == 2)
        {
            result = operate( a , b , pt_product );
            cout << a << "x" << b << "=" << result;
        }
        
    }

    return 0;

}


double ratio(double a, double b)
{
    double c=a/b;
    return c;
}

double product(double a, double b)
{
    double c=a*b;
    return c;
}

double operate(double a, double b, double (*funcall)(double, double))
{
    double c=(*funcall)(a,b);
    return c;
}
Because of lines 21 and 22:
1
2
double * pt_ratio;
double * pt_product;

line 31 treats pt_ratio as if it were a variable, instead of a function.
same with 36.

Removing 21 and 22 caused the program to run fine for me.

Thanks. I do still get

Expected '; ' after top level declarator

at line 47:

double ratio(double a, double b)
Last edited on
Topic archived. No new replies allowed.