vector<T> m;
is a private member in a template class.
template<class T>
bool Matrix_lt<T> :: isNotZero(T val) {
return val != 0;
}
is a private function in the same template class.
template<class T>
int Matrix_lt<T> :: calL0Norm() {
int count = count_if(m.begin(), m.end(), isNotZero<T>);//ERROR
}
is a public function in the same template class.
ERROR: expected primary-expression before '>' token. Why??
isNotZero<T>
is a member function, so it has an implicit first parameter for this
. You need a unary functor, so you will need to use std::bind
, to bind this
as the first parameter.
You also need to refer to the function as &Matrix::isNotZero<T>
. So,
using namespace std::placeholders;
auto f = std::function<bool(const T&)> f = std::bind(&Matrix::isNotZero<T>,
this, _1);
and use f
as the functor in count_if
.
Alternatively, use a lambda:
int count = count_if(m.begin(), m.end(),
[this](const T& val) { return this->isNotZero<T>(val);});