1

So I have been trying and battling with this for a few hours now. I am relatively new to c++ but I read about function pointers and it looked pretty useful. Essentially, what I am attempting to do below is to pass two integer values to the function foo and then multiply it by 10. However, I keep getting the following error: "invalid conversion from 'int' to 'int (*)(int, int)' [-fpermissive]".

#include <iostream>

using namespace std;

int foo(int nX, int nY){
    return nX*nY;
}


int multTen(int a, int b, int (*Fn)(int, int)){
    return 10*Fn(a,b);
}

int main(){
    cout << multTen(3,4,foo(3,4)) << endl;
}

Your kind help in this regard is highly appreciated. :)

3 Answers 3

3
cout << multTen(3,4,foo(3,4)) << endl;
                 // ^^^^^^^ calling function foo and it's return value is passed 
                 // to multTen

The problem is that you are calling the mulTen with the return value of foo as a parameter instead of passing a pointer to it.

Sign up to request clarification or add additional context in comments.

Comments

2

Did you mean this?

cout << multTen(3,4,foo) << endl;

Comments

2

Omit the (3,4) on foo. In your code sample, you are calling the foo function with the parameters (3,4) and passing the return value to multTen.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.