简体   繁体   English

将函数作为参数传递给C ++中的方法

[英]Passing a function as a parameter to a method in C++

I want to make a method for a (mathematical) matrix class to process objects with the function given in parameter, but I'm stuck with function pointers! 我想为一个(数学)矩阵类创建一个方法,用参数中给出的函数处理对象,但是我坚持使用函数指针!

My code: 我的代码:

#include <iostream>
class Matrix{
  public:
    Matrix(int,int);
    ~Matrix();
    int getHeight();
    int getWidth();
    float getItem(int,int);
    void setItem(float,int,int);
    float getDeterminans(Matrix *);
    void applyProcessOnAll(float (*)());
  private:
    int rows;
    int cols;
    float **MatrixData;
};

Matrix::Matrix(int width, int height){
  rows = width;
  cols = height;
  MatrixData = new float*[rows];
  for (int i = 0;i <= rows-1; i++){
    MatrixData[i] = new float[cols];
  }
}

Matrix::~Matrix(){}
int Matrix::getWidth(){
  return rows;
}
int Matrix::getHeight(){
  return cols;
}
float Matrix::getItem(int sor, int oszlop){
  return MatrixData[sor-1][oszlop-1];
}
void Matrix::setItem(float ertek, int sor, int oszlop){
  MatrixData[sor-1][oszlop-1] = ertek;
}
void Matrix::applyProcessOnAll(float (*g)()){
  MatrixData[9][9]=g(); //test
}
float addOne(float num){ //test
  return num+1;
}

int main(void){
  using namespace std;
  cout << "starting...\r\n";
  Matrix A = Matrix(10,10);
  A.setItem(3.141,10,10);
  A.applyProcessOnAll(addOne(3));
  cout << A.getItem(10,10);
  cout << "\r\n";
  return 0;
}

The compiler gives me this error: error: no matching function for call to 'Matrix::applyProcessOnAll(float)' note: candidate is: note: void Matrix::applyProcessOnAll(float ( )()) note: no known conversion for argument 1 from 'float' to 'float ( )()' 编译器给我这个错误:错误:没有匹配函数调用'Matrix :: applyProcessOnAll(float)'注意:候选者是:注意:void Matrix :: applyProcessOnAll(float( )())注意:参数没有已知的转换1从'float'到'float( )()'

Thanks for the help! 谢谢您的帮助!

Now it works! 现在它有效! Thanks! 谢谢!

MODIFIED PARTS 改装零件

void Matrix::applyProcessOnAll(float (*proc)(float)){
    for(int i = 0; i <= rows-1;i++)
        for(int j = 0; j <= cols-1;j++)
            MatrixData[i][j]=proc(MatrixData[i][j]);
}

and in main: 在主要:

A.applyProcessOnAll(*addOne);

Because your float (*g)() does not take an argument and your addOne takes a float argument. 因为你的float (*g)()不接受参数,你的addOne接受一个float参数。 Change your function pointer to float (*g)(float) and now it should work. 将函数指针更改为float (*g)(float) ,现在它应该可以工作。

Also you should assign the function to the pointer, and not call it. 您还应该将函数分配给指针,而不是调用它。

A.applyProcessOnAll(&addOne, 3); //add args argument to `applyProcessOnAll` so you can call `g(arg)` inside.

You have two problems. 你有两个问题。

The first is the one pointed out by Tony The Lion : you specified that the function shouldn't take any parameters but you're using a function that takes a single parameter. 第一个是托尼狮子指出的那个 :你指定该函数不应该采用任何参数,但你使用的函数只需要一个参数。

The second is that you're calling applyProcessOnAll with the results of a function call, not a pointer to the function. 第二个是你使用函数调用的结果调用applyProcessOnAll ,而不是指向函数的指针。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM