How to pass a function as a parameter in c++?

Viewed 158

I have this file 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

I edited the code and now I have a different error about the same function apply(Function function)


ps: if there are any more informaion needed about my class, please tell me in the comments, and don't just close my question, without asking me to edit it first..

errors:

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>*’

code that used the function:

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

the thing is I must not change **

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

the solution was to declare the function in the class like this

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

and then write it's body and use it this way:

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