s*******o 发帖数: 392 | 1 刚开始看www.cplusplus.com上的tutorial文档,在73页写到了关于function pointer
的用
法,可是我录入后报错,希望大家帮忙看看
#include
using namespace std;
int addition (int a, int b)
{
return(a + b);
}
int subtraction (int a, int b)
{
return(a - b);
}
int operation(int x, int y, int (* functocall(int x, int y)))
{
int g;
g = (*functocall(x, y));
return(g);
}
int main()
{
int m;
int (*minus)(int, int) = subtraction;
m = operation(7 , 5, minus);
std::cout << m;
system("PAUSE");
return 0;
}
他报的错误时
d:\backup\dropboxsas\dropbox\coding\vs2010\helloworld\addfunction\
addfunction.cpp(
27): error C2664: 'operation' : cannot convert parameter 3 from 'int (__
cdecl *)
(int,int)' to 'int *(__cdecl *)(int,int)'
1> This conversion requires a reinterpret_cast, a C-style cast or
function-style cast
可是我写的和他的例子一摸一样啊,大虾帮下,先谢谢了 | X****r 发帖数: 3557 | 2 是(*functocall)(int x, int y),不是(* functocall(int x, int y))
注意括号位置。
pointer
【在 s*******o 的大作中提到】 : 刚开始看www.cplusplus.com上的tutorial文档,在73页写到了关于function pointer : 的用 : 法,可是我录入后报错,希望大家帮忙看看 : #include : using namespace std; : int addition (int a, int b) : { : return(a + b); : } : int subtraction (int a, int b)
| z****e 发帖数: 2024 | 3 还有
g = (*functocall(x, y));
是
g=(*functocall) (x, y);
【在 X****r 的大作中提到】 : 是(*functocall)(int x, int y),不是(* functocall(int x, int y)) : 注意括号位置。 : : pointer
|
|