好吧,基本上,我已经浏览了所有有关此问题的问答,但找不到我的答案。
这是我的代码,它将对平方,立方,四分之一的数字进行计算。但是,尝试输出答案时出现错误。
#include <iostream>
using namespace std;
int square (int); //n^2
int cube (int); //n^3
int fourth (int); //n^4
void powerN(int x[], const int sizex, void(*select)(int))
{
cout<< x[sizex];
cout<<" to the power of "<<sizex+2<<" is ";
cout<<(*select)(x[sizex])<<endl;
}
int square (int a)
{
return a*a;
}
int cube (int b)
{
return b*b*b;
}
int fourth (int c)
{
return c*c*c*c;
}
int main()
{
int a[3]={3,4,5};
for (int aSize=0;aSize<3;a++){
powerN (a, aSize, square);
powerN (a, aSize, cube);
powerN (a, aSize, fourth);
}
}
函数指针参数select
的类型不正确。您将其返回类型声明为void
,无法进行cout
编辑。返回值应为类型int
。
void powerN(int x[], const int sizex, int(*select)(int))
// ~~~
该对环main()
,a++
应该是aSize++
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句