Hi I'm doing a templated matrix class,
and I'd like to be able to pass a function pointer to a function that uses the rows.
template<typename T>
T sum(Array<T> &a){ // a simple function that return the sum of an Array
T res=0;
for(int i=0;i<a.length();i++)
res+=a(i);
return res;
}
template<typename T>
Array<T> getrow(Matrix<T> &m,int r){// a simple function that return a row from a matrix
Array<T> retAry(m.y());
for(int i=0;i<m.y();i++)
retAry(i) = m(r,i);
return retAry;
}
//this is the function I'd like to implement
template<typename T>
Array<T> apply(Matrix<T> &m,T (*fun)(Array<T>)){
Array<T> retA(m.x());
for (int i=0;i<m.x();i++){
Array<T> r = getrow(m,i);
retA(i) = (*fun)(r);
}
return retA;
}
//same as above but with out the function pointer
template<typename T>
Array<T> applyThisWorks(Matrix<T> &m){
Array<T> retA(m.x());
for (int i=0;i<m.x();i++){
Array<T> r = getrow(m,i);
retA(i) = sum(r);
}
return retA;
}
int main(){
Matrix<int> m(3,5);
fillIt(m);
m.print();
Array<int> var = applyThisWorks(m);
//Array<int> var = apply(m,&sum);//how do I call the function?
cout <<"this is the rowwise sum:"<<var;
}
My code is of cause a lot more, I can post it,
if you need it.
Everything is working, and when I use the nonfunction pointer I get the desired output.
dim is: (3,5)
0 1 2 3 4
5 6 7 8 9
10 11 12 13 14
this is the rowwise sum:10 35 60
But If I try to compile the desired general version i get this error
matrix.cpp: In function ‘int main()’:
matrix.cpp:230: error: no matching function for call to ‘apply(Matrix<int>&,
<unresolved overloaded function type>)’
matrix.cpp:206: note: candidates are: Array<U> apply(Matrix<T>&, T (*)(Array<U>))
[with T = int]
Is there someway I can coerce the function type,
or is it even possible to do this?
thanks in advance