I was reading a tutorial on this site about pointers to functions, and I wanted to create something on my own.
I wanted to make a simple function that adds two numbers, and then prints it out via another function; but for some reason it's not working. I'm using Xcode, and I get an error message saying "No matching function for call to 'sum'".
#include <iostream>
usingnamespace std;
template <class type>
void print(type a) {
cout << a << endl;
}
template <class type>
void sum(type a, type b, type (*function)(type)) {
(*function)(a+b);
}
int main() {
int i1=1, i2=3;
float f1=1.4, f2=14.9;
sum(i1, i2, print); // This is where I get the error message.
return 0;
}
This means that the function passed needs to have the same return type as 'type'. You might want to change this to 'void' if you want your print function to be able to be passed.
Second of all, you are passing a pointer to a function, so in when you pass 'print' as the argument you need to get it's pointer address: