C++:STL 错误 C2064

发布于 2024-09-12 05:43:16 字数 1925 浏览 2 评论 0原文

我正在尝试使用 STL,但以下内容无法编译。 ma​​in.cpp

#include <set>
#include <algorithm>

using namespace std;

class Odp
{
public:

    set<int> nums;

    bool IsOdd(int i)
    {
        return i % 2 != 0;
    }

    bool fAnyOddNums()
    {
        set<int>::iterator iter = find_if(nums.begin(), nums.end(), &Odp::IsOdd);
        return iter != nums.end();
    }
};

int main()
{
    Odp o;
    o.nums.insert(0);
    o.nums.insert(1);
    o.nums.insert(2);
}

错误是:

error C2064: term does not evaluate to a function taking 1 arguments
1>          c:\program files\microsoft visual studio 10.0\vc\include\algorithm(95) : see reference to function template instantiation '_InIt std::_Find_if<std::_Tree_unchecked_const_iterator<_Mytree>,_Pr>(_InIt,_InIt,_Pr)' being compiled
1>          with
1>          [
1>              _InIt=std::_Tree_unchecked_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1>              _Mytree=std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>,
1>              _Pr=bool (__thiscall Odp::* )(int)
1>          ]
1>          main.cpp(20) : see reference to function template instantiation '_InIt std::find_if<std::_Tree_const_iterator<_Mytree>,bool(__thiscall Odp::* )(int)>(_InIt,_InIt,_Pr)' being compiled
1>          with
1>          [
1>              _InIt=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1>              _Mytree=std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>,
1>              _Pr=bool (__thiscall Odp::* )(int)
1>          ]

我做错了什么?

I'm trying to use STL, but the following doesn't compile. main.cpp:

#include <set>
#include <algorithm>

using namespace std;

class Odp
{
public:

    set<int> nums;

    bool IsOdd(int i)
    {
        return i % 2 != 0;
    }

    bool fAnyOddNums()
    {
        set<int>::iterator iter = find_if(nums.begin(), nums.end(), &Odp::IsOdd);
        return iter != nums.end();
    }
};

int main()
{
    Odp o;
    o.nums.insert(0);
    o.nums.insert(1);
    o.nums.insert(2);
}

The error is:

error C2064: term does not evaluate to a function taking 1 arguments
1>          c:\program files\microsoft visual studio 10.0\vc\include\algorithm(95) : see reference to function template instantiation '_InIt std::_Find_if<std::_Tree_unchecked_const_iterator<_Mytree>,_Pr>(_InIt,_InIt,_Pr)' being compiled
1>          with
1>          [
1>              _InIt=std::_Tree_unchecked_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1>              _Mytree=std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>,
1>              _Pr=bool (__thiscall Odp::* )(int)
1>          ]
1>          main.cpp(20) : see reference to function template instantiation '_InIt std::find_if<std::_Tree_const_iterator<_Mytree>,bool(__thiscall Odp::* )(int)>(_InIt,_InIt,_Pr)' being compiled
1>          with
1>          [
1>              _InIt=std::_Tree_const_iterator<std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>>,
1>              _Mytree=std::_Tree_val<std::_Tset_traits<int,std::less<int>,std::allocator<int>,false>>,
1>              _Pr=bool (__thiscall Odp::* )(int)
1>          ]

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

陈年往事 2024-09-19 05:43:16

它需要声明为静态:

static bool IsOdd(int i)

否则,您将要求 find_if 在没有实例的情况下调用实例方法。

It needs to be declared static:

static bool IsOdd(int i)

Otherwise, you'd be asking find_if to call an instance method without an instance.

牵你的手,一向走下去 2024-09-19 05:43:16

问题是您正在传递一个指向成员函数的指针。要调用该函数,您还需要一个指向该函数的指针,但 find_if 不允许您传递它。解决方案是使用函数对象包装它,请参阅 Boost Bind (http://www.boost.org/doc/libs/1_43_0/libs/bind/bind.html)和Boost函数(http://www.boost.org/doc/libs/1_37_0/doc/html/function.html)。

The problem is you are passing a pointer to member function. To call that function you would also need a pointer to this but the find_if doesn't let you to pass it. A solution is to wrap it using a function object, see Boost Bind (http://www.boost.org/doc/libs/1_43_0/libs/bind/bind.html) and Boost Function (http://www.boost.org/doc/libs/1_37_0/doc/html/function.html).

水染的天色ゝ 2024-09-19 05:43:16

IsOdd 不以任何方式使用类的内部结构,因此不要将其设为成员函数。相反,将其作为一个独立的功能拉出来。然后您可以使用 &IsOdd 调用 find_if

但是,更进一步将其定义为函数对象是有好处的:

#include <functional>

struct IsOdd : public unary_function<int, bool>
{
    bool operator()(int i) const { return i % 2 != 0; }
};

然​​后使用 IsOdd() 调用 find_if内联代码 在 find_if 循环中,而不是取消引用函数指针并进行函数调用。

IsOdd does not use the class's internals in any way, so don't make it a member function. Instead, pull it out as a standalone function. Then you can call find_if with &IsOdd.

However, there is a benefit to taking things a step further and defining it as a function object:

#include <functional>

struct IsOdd : public unary_function<int, bool>
{
    bool operator()(int i) const { return i % 2 != 0; }
};

Then calling find_if with IsOdd() will inline the code within the find_if loop instead of dereferencing a function pointer and making a function call.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文