如何在C++中将函数作为参数传递?

3

我有这个文件Matrix.h:

#ifndef MTM_MATRIX_H_
#define MTM_MATRIX_H_
#include <assert.h>


namespace mtm
{
 
 template<class T>
 
    class Matrix
 {
     T** data;
     int col;
     int row;
    public:
          Matrix(int row,int col, T intial=0);
          T& operator()(int row, int col);
          const T& operator() (int row, int col) const;
          //some functions
        template<typename Function>
        Matrix<T> apply(Function function);

};
//ctor
template<class T>
 Matrix<T>::Matrix(int row,int col, T intial):data(new T*[row]), col(col), row(row)
        {
            
            for (int i = 0; i < row; i++)
                {
                     data[i] = new T[col];
                }
             for(int i=0;i<row;i++)
            {
                for(int j=0;j<col;j++)
                {
                    data[i][j]=intial;
                }
            }
         }
template<class T>
T& Matrix<T>::operator()(int row, int col)
{
    return this->data[row][col];
}
template<class T>
const T& Matrix<T>::operator() (int row, int col) const
{
    return this->data[row][col];
}
template<class T>
template<typename Function>
Matrix<T> Matrix<T>::apply(Function function) 
{
    Matrix<T> new_mat(this->row,this->col);
    for(int i=0;i<new_mat.row;i++)
    {
        for(int j=0;j<new_mat.col; j++)
        {
            new_mat(i,j)= function(this->data[i][j]);
        }
    }
    return new_mat;
}
}
  
#endif

我编辑了代码,现在同一函数 apply(Function function) 出现了不同的错误。


附言:如果需要更多关于我的类的信息,请在评论中告诉我,不要仅仅关闭我的问题而不先要求我进行编辑。

错误:

test_partB.cpp: In function ‘int main()’:
test_partB.cpp:54:58: error: no matching function for call to ‘mtm::Matrix<int>::apply(Square) const’
         const mtm::Matrix<int> mat_2=mat_1.apply(Square());
                                                          ^
test_partB.cpp:54:58: note: candidate is:
In file included from test_partB.cpp:3:0:
Matrix.h:593:11: note: mtm::Matrix<T> mtm::Matrix<T>::apply(Function) [with Function = Square; T = int] <near match>
 Matrix<T> Matrix<T>::apply(Function function)
           ^
Matrix.h:593:11: note:   no known conversion for implicit ‘this’ parameter from ‘const mtm::Matrix<int>*’ to ‘mtm::Matrix<int>*’

使用该函数的代码:

//this is **
const mtm::Matrix<int> mat_1(1,2,4);
        const mtm::Matrix<int> mat_2=mat_1.apply(Square());

问题在于我不能改变 **

class Square { 
    public: 
        int operator()(int val){ 
          return val*val; 
    } 
}; 

2
请提供一个最小化完整可复制演示 [MCVE]。你是如何调用它的? - Kevin
5
apply没有被标记为const,因此无法在一个const矩阵上调用。 - Miles Budnek
1
namespace mtm 的闭合 } 在哪里?或者 apply 函数的定义在哪里?请创建一个适当的 [mcve],目前还缺少细节。 - UnholySheep
1
你复制粘贴出了问题吗?现在你有一个模板化的命名空间和一个 IntMatrix 类,但是不再有任何定义矩阵模板类的 Matrix 模板类。 - Kevin
3
其他所有问题与标题问题无关。this.row,this.col 是无效的,new_mat(i,j) 是无效的,this(i,j) 超出了这个世界,function()(x) 不是通常调用名为 functtion 的函数的方式。 - n. m.
显示剩余18条评论
1个回答

0
解决方案是在类中声明函数,就像这样:
 template<typename Function>
        Matrix<T> apply(Function function);

然后编写它的主体并以这种方式使用:

template<class T>
template<typename Function>
Matrix<T> Matrix<T>::apply(Function function) 
{
  int k=3;
  int n=function(k);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接