。函数指针数组。不懂错误的意思
#include<iostream>using namespace std;
void square(float &x, float &y)
{
x=x*x;
y=y*y;
}
void cube(float &x, float &y)
{
x=x*x*x;
y=y*y*y;
}
void print(float &x, float &y)
{
cout<<"长:"<<x<<"\t"<<"宽:"<<y<<endl;
}
void swap(float &x, float &y)
{
float z;
z=x;
x=y;
y=z;
}
int main(void)
{
float a=2,b=3;
char choice='0';
int i;
void (*p[5])(float &, float &);
for (i=0; i<5; i++)
{
bool quit = false;
cout<<"(0)退出,(1)平方,(2)立方,(3)交换x和y的值:";
cin>>choice;
switch (choice)
{
case '0': quit=true; break;
case '1': p[i]=square; break;
case '2': p[i]=cube; break;
case '3': p[i]=swap; break; //错误指向的地方
default:p[i]=0;
}
if (quit)break;
if (p[i]==0)
{
cout<<"请输入0到3之间的数字:";
i=i-1;
continue;
}
cout<<"第"<<i<<"次执行,到第5次结束\n";
cout<<"初始值\n";
print(a,b);
cout<<"现在调用函数指针数组p["<<i<<"]所指向的函数...\n";
p[i](a,b);
cout<<"运算后\n";
print(a,b);
}
return 0;
}
/*
——————————————————————————————————————
E:\c++\108函数指针数组\01.cpp(40) : error C2563: mismatch in formal parameter list
E:\c++\108函数指针数组\01.cpp(40) : error C2568: '=' : unable to resolve function overload
could be 'void __cdecl swap(float &,float &)'
E:\c++\108函数指针数组\01.cpp(17) : see declaration of 'swap'
or 'void __cdecl std::swap(float &,float &)'
d:\vc98\include\xutility(99) : see declaration of 'swap'
or 'void __cdecl std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::swap(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std:
:char_traits<char>,class std::allocator<char> > &)'
d:\vc98\include\xstring(378) : see declaration of 'swap'
or 'void __cdecl std::swap(_Ty &,_Ty &)'
d:\vc98\include\xutility(99) : see declaration of 'swap'
Error executing cl.exe.
01.obj - 2 error(s), 0 warning(s)
——————————————————————————————————————
*/