我被告知要创建函数模板,这将需要 4 个参数:
- 指针
- 参考
- 指向数组的指针
- 函数指针
如何执行此任务?我在尝试:
#include <iostream>
using namespace std;
int nothing(int a)
{
return a;
}
template<typename T> T func(int *L, int &M, char *K, int (*P)(int))
{
cout << L << "," << M << "," << K[0] << "," << P() << endl;
return 0;
}
int main()
{
int x = 3;
int *z = &x;
int &y = x;
char c[3];
int (*pf)(int) = nothing;
cout << "some result of func" << func(z, y, c, pf) << endl;
system("pause");
return 0;
}
这给了我“没有匹配的功能,我猜是‘pf’。现在我也无法控制在 pf 中传递什么,或者我错了吗?