![](/img/trans.png)
[英]C++ error: expected primary-expression before ‘.’ token
[英]C++ ERROR about count_if: expected primary-expression before
vector<T> m;
是模板类中的私有成员。
template<class T>
bool Matrix_lt<T> :: isNotZero(T val) {
return val != 0;
}
是同一模板类中的私有函数。
template<class T>
int Matrix_lt<T> :: calL0Norm() {
int count = count_if(m.begin(), m.end(), isNotZero<T>);//ERROR
}
是同一模板类中的公共函数。
错误:“>”令牌之前的预期主表达式。 为什么??
isNotZero<T>
是一个成员函数,所以它具有用于一个隐式的第一个参数this
。 您需要一元函子,因此将需要使用std::bind
bind this
绑定为第一个参数。
您还需要将该函数称为&Matrix::isNotZero<T>
。 所以,
using namespace std::placeholders;
auto f = std::function<bool(const T&)> f = std::bind(&Matrix::isNotZero<T>,
this, _1);
并在count_if
中将f
用作函子。
或者,使用lambda:
int count = count_if(m.begin(), m.end(),
[this](const T& val) { return this->isNotZero<T>(val);});
isNotZero
是成员函数。 您不能这样使用。 使用lambda:
template<class T>
int Matrix_lt<T> :: calL0Norm() {
int count = count_if(m.begin(),
m.end(),
[this](T const & val) { return this->isNotZero(v);} );
}
或使用std::bind
。
这里有两个问题。
根据您的定义, isNotZero
不是模板函数,它是模板类的成员函数。
您不能使用模板参数引用非模板函数。 使用int count = count_if(m.begin(), m.end(), isNotZero);
isNotZero
是一个非静态成员函数。 您不能通过这样的成员函数。
我认为在这种情况下,可以将isNotZero
为静态成员函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.