在另一个函数参数中函数

Function in another function parameters

本文关键字:函数 参数 另一个      更新时间:2023-10-16

好吧,基本上我已经浏览了所有Q& a关于此的Q&无法找到我的答案。

这是我的代码,在该代码中,它将对广场,立方体,数字的四分之一进行计算。但是,试图输出答案时存在错误。

#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);
    }
}
  1. 功能指针参数select的类型不正确。您将其返回类型声明为void,不能是cout ED。返回值应为类型int

    void powerN(int x[], const int sizex, int(*select)(int))
    //                                    ~~~
    
  2. main()中的for循环, a++应该是 aSize++

live