//C版能运行,VC6.0
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define bool unsigned char
#define boolean bool
#define true 1
#define false 0
typedef int *(*sum)(int* a,int* b);
typedef int (*huiwen)(char *str);
int *sum_ab(void *pa,void *pb)
{
int pab_sum=(*(int *)pa+ *(int *)pb);
int *P_sum = &pab_sum;
return P_sum;
}
int wen_str(void *str)
{
char *pstr=(char*)str;
int n= strlen(pstr);
int i,j=n-1;
int vbool=false;
if(n>=2) vbool=true;
for (i=0;i<n/2;i++)
{
if(pstr[i]==pstr[j--])
{
vbool=true;
continue ;
}
else
{
vbool =false ;
break;
}
}
return vbool;
}
void my_bool_callback(huiwen my_bool_fun,char *str)
{
int vbool;
vbool = (int)my_bool_fun(str);
if(vbool==true)
printf("true\n");
else
printf("false\n");
}
void my_add_callback(sum my_add_func, void *m, void *n)
{
int *pIntResults = NULL;
pIntResults = (int *)my_add_func(m, n);
printf("result = %d\n",*pIntResults);
}
void main()
{
int x=3,y=4;
int s=wen_str("fasfd");
huiwen my_bool_fun;
my_bool_fun=wen_str;
printf("%d\n",my_bool_fun("fasaf"));//
my_add_callback(sum_ab, &x, &y);
my_bool_callback(wen_str,"a");
}
//C++版不能运行,VS2008
#include <iostream>
using namespace std;
#include <string.h>
#define bool unsigned char
#define boolean bool
#define true 1
#define false 0
typedef int *(*sum)(int* a,int* b);
typedef int (*huiwen)(char *str);
int *sum_ab(void *pa,void *pb)
{
int pab_sum=(*(int *)pa+ *(int *)pb);
int *P_sum = &pab_sum;
return P_sum;
}
int wen_str(void *str)
{
char *pstr=(char*)str;
int n= strlen(pstr);
int i,j=n-1;
int vbool=false;
if(n>=2) vbool=true;
for (i=0;i<n/2;i++)
{
if(pstr[i]==pstr[j--])
{
vbool=true;
continue ;
}
else
{
vbool =false ;
break;
}
}
return vbool;
}
void my_bool_callback(huiwen my_bool_fun,char *str)
{
int vbool;
vbool = (int)my_bool_fun(str);
if(vbool==true)
cout<<"true\n";
else
cout<<"false\n";
}
void my_add_callback(sum my_add_func, void *m, void *n)
{
int *pIntResults = NULL;
pIntResults = (int *)my_add_func((int*)m, (int*)n);
cout<<"result = "<<*pIntResults<<endl;
}
void main()
{
int x=3,y=4;
int s=wen_str("fasfd");
huiwen my_bool_fun;
my_bool_fun=wen_str;
printf("%d\n",my_bool_fun("fasaf"));//
my_add_callback(sum_ab, &x, &y);
my_bool_callback(wen_str,"a");
}
错误提示:
1>e:\mycode\filter\filter\filter.cpp(81) : error C2440: “=”: 无法从“int (__cdecl *)(void *)”转换为“huiwen”
1> 该转换要求 reinterpret_cast、C 样式转换或函数类型转换
1>e:\mycode\filter\filter\filter.cpp(83) : error C2664: “my_add_callback”: 不能将参数 1 从“int *(__cdecl *)(void *,void *)”转换为“sum”
1> 在匹配目标类型的范围内没有具有该名称的函数
1>e:\mycode\filter\filter\filter.cpp(84) : error C2664: “my_bool_callback”: 不能将参数 1 从“int (__cdecl *)(void *)”转换为“huiwen”
1> 在匹配目标类型的范围内没有具有该名称的函数
解决方法:?