- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个继承自 unary_function 的仿函数类:
template<class T>
class Matcher : public std::unary_function<T, bool>
{
private:
int m_match;
public:
Matcher(int valToMatch) : m_match(valToMatch) { };
bool operator() (T toTest)
{
return T.prop == m_match;
}
}
使用这些东西之一的函数:
void DoStuff(std::unary_function<ThisType, bool> & pred,
vector<ThisType> & stuffToTest)
{
for(vector<ThisType>::iterator it = stuffToTest.begin();
it != stuffToTest.end(); ++it)
{
if(pred(*it)) // <<< Compiler complains here
{
// do stuff
}
}
}
原始调用函数:
Matcher myMatcher<ThisType>(n);
// have vector<ThisType> myStuff
DoStuff(myMatcher, myStuff);
据我所知,我有一个模板仿函数,我正在用它构造一个 ThisType 类型的实例,我将它传递给需要 unary_function 参数的函数,并使用 ThisType 的实例调用。
但是编译器提示说“term 的计算结果不是带有 1 个参数的函数”。
我错过了什么?
最佳答案
这是因为即使你将派生类对象传递给函数,函数参数仍然是 std::unary_function
没有成员 operator()
接受一个参数。因此错误。
我建议您将函数更改为函数模板:
template<typename F>
void DoStuff(F && pred, vector<ThisType> & stuffToTest)
{
for(auto it = stuffToTest.begin(); it != stuffToTest.end(); ++it)
{
if(pred(*it))
{
// do stuff
}
}
}
关于c++ - 从 unary_function 继承的谓词仿函数,它不是一个接受 1 个参数的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14261949/
代码应该解释我的困难。虽然代码本身没什么意义,但我打算在 MyClass 中添加容器,并使用带有成员函数的算法。 #include #include #include using namespa
使用 VS2005 在 Windows 上工作并努力理解我收到的错误消息。如果之前有人问过这个问题,我很抱歉。我找不到它。 我正在测试的类: #include using std::unary_fu
我有一个看起来像这样的类。 class A { public: void doSomething(); } 我有一组这些类。我想对数组中的每个项目调用 doSomething() 。使用算法
我遇到了类模板 std::unary_function 和 std::binary_function。 template struct unary_function { typedef Ar
我只是在为一组仿函数寻找一个方便的基类,这些仿函数基于获取和 int 并返回 void。 考虑使用 std/functional 仿函数基本上是 unary_function与 operator()
这个问题在这里已经有了答案: Where and why do I have to put the "template" and "typename" keywords? (8 个答案) 关闭 9
我阅读了有关二元和一元函数的教程。我了解它们的结构,但我无法想象在哪种情况下我需要这些功能。你能举个例子来说明它们的用法吗? http://www.cplusplus.com/reference/st
这是导致我出现一些问题的代码,尝试构建并获取错误: 'unary_function base class undefined' and 'unary_function' is not a member
当如下检查从 std::unary_function 派生的仿函数时 struct IsInterestingMsg : public std::unary_function Lint 弹出以下信息/
我有一个继承自 unary_function 的仿函数类: template class Matcher : public std::unary_function { private: int
这是我的代码: using namespace std; class Pixel { public: bool AreSamplesIdentical() const {
我发现 binary_function 已从 C++11 中删除。我想知道为什么。 C++98: template struct less : binary_function { bool o
std::unary_function特性在 c++11 中被弃用并在 c++17 中被删除。但是使用 c++17 编译器标志,这段代码仍然可以编译: struct less_than_7 : std
binder2nd 派生自 unary_function。 以下代码片段不起作用: greater g; //greater is derived from binary_function /
我是一名优秀的程序员,十分优秀!