【问题标题】:How to pass a function as a parameter in c++?如何在c ++中将函数作为参数传递?
【发布时间】:2020-06-30 15:01:48
【问题描述】:

我有这个文件 矩阵.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)


ps:如果需要关于我的课程的更多信息,请在 cmets 中告诉我,不要直接关闭我的问题,而不要求我先对其进行编辑。..

错误:

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; 
    } 
}; 

【问题讨论】:

  • 请提供minimal reproducible example。你怎么称呼它?
  • apply 未标记为const,因此无法在const 矩阵上调用。
  • namespace mtm 的结束 } 在哪里?还是apply的函数定义?请创建一个正确的minimal reproducible example,这仍然缺少详细信息
  • 你搞砸了你的复制粘贴吗?现在您有了一个模板化命名空间 (?) 和一个 IntMatrix 类,并且不再在任何地方定义 Matrix 模板类。
  • 所有其他问题也与名义问题无关。 this.row,this.col 无效,new_mat(i,j) 无效,this(i,j) 不在这个世界上,function()(x) 不是您通常调用名为 functtion 的函数的方式。

标签: c++ function templates parameters


【解决方案1】:

解决方案是像这样在类中声明函数

 template<typename Function>
        Matrix<T> apply(Function function);

然后写它的body,这样使用:

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

【讨论】:

    猜你喜欢
    • 2020-02-10
    • 2016-10-27
    • 1970-01-01
    • 1970-01-01
    • 2013-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多