std::mem_fun_ref 问题:作为成员函数调用时,函子被破坏

发布于 2024-10-30 23:41:36 字数 2257 浏览 6 评论 0原文

问题是下面的代码片段出现编译器错误。

这是一个非常简单的程序,用随机整数填充列表并递增每个元素。我使用 std::for_each 调用函子来增加我的集合中的每个成员并全部编译。重点关注 main() 中的 for_each。接下来,我只需更改 for_each 来调用 List::increment 而不是函子 Increment(请注意 main 中注释掉的代码)。对于此任务,我使用 mem_fun_ref 因为我处理的不是指针的集合而是类的集合。另外我认为我不需要bind1st 或bind2nd。

这是我的编译器错误:

/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h: In function _Function std::for_each(_InputIterator, _InputIterator, _Function) [with _InputIterator = std::_List_iterator, _Function = std::mem_fun1_ref_t]': blahblah.cpp:53:从此处实例化

/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h: 158:错误:与 (std::mem_fun1_ref_t) (unsigned int&)'

/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_function.h:826 的调用不匹配:注意:候选者是: void std::mem_fun1_ref_t::operator()(_Tp&, _Arg) const [with _Tp = List, _Arg = unsigned int&]

#include <list>
#include <string>
#include <algorithm>
#include <functional>
#include <iostream>

static const unsigned int LIST_SIZE = 25;

typedef std::list<unsigned int> List_t;

class List
{
    public:
        List() {};
        ~List() {};
        void push_back(unsigned int in) {m_list.push_back(in);};
        List_t::iterator begin( ) {m_list.begin();}
        List_t::iterator end( ) {m_list.end();};
        void print_list ( ) const
        {
            std::cout << std::endl;
            for (List_t::const_iterator iter = m_list.begin(); iter != m_list.end(); ++iter)
            {
                std::cout << *iter << ' ';
            }
            std::cout << std::endl << std::endl;

        }
        void increment( unsigned int & input)
        {
            ++input;
        }
    private:
        List_t m_list;
};
struct Increment{
    void operator ()(unsigned int &input)
    {
       ++input;
    }
};

int main()
{
    List l;
    for (unsigned int i= 0; i <= LIST_SIZE; i++)
    {
        unsigned int x = rand() % 100;
        l.push_back(x);
    }
    l.print_list(); // pre incremented
    //for_each(l.begin(),l.end(),Increment());
    for_each(l.begin(),l.end(),std::mem_fun_ref(&List::increment));
    l.print_list(); // incremented
    return 1;
}

The problem is compiler errors with the code snippet below.

Here's a very simple program to fill a list with random integers and increment each element. I use a std::for_each call to a functor to increment each member of my collection and all compiled. Focus on the for_each in main(). Next I simply alter the for_each to call List::increment rather than the functor Increment (notice the commented out code in main). For this task I use mem_fun_ref since I am not dealing with a collection of pointers but of classes. Also I don't think I need bind1st or bind2nd.

Here's my compiler errors:

/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h: In function _Function std::for_each(_InputIterator, _InputIterator, _Function) [with _InputIterator = std::_List_iterator, _Function = std::mem_fun1_ref_t]': blahblah.cpp:53: instantiated from here

/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_algo.h:158: error: no match for call to (std::mem_fun1_ref_t) (unsigned int&)'

/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_function.h:826: note: candidates are: void std::mem_fun1_ref_t::operator()(_Tp&, _Arg) const [with _Tp = List, _Arg = unsigned int&]

#include <list>
#include <string>
#include <algorithm>
#include <functional>
#include <iostream>

static const unsigned int LIST_SIZE = 25;

typedef std::list<unsigned int> List_t;

class List
{
    public:
        List() {};
        ~List() {};
        void push_back(unsigned int in) {m_list.push_back(in);};
        List_t::iterator begin( ) {m_list.begin();}
        List_t::iterator end( ) {m_list.end();};
        void print_list ( ) const
        {
            std::cout << std::endl;
            for (List_t::const_iterator iter = m_list.begin(); iter != m_list.end(); ++iter)
            {
                std::cout << *iter << ' ';
            }
            std::cout << std::endl << std::endl;

        }
        void increment( unsigned int & input)
        {
            ++input;
        }
    private:
        List_t m_list;
};
struct Increment{
    void operator ()(unsigned int &input)
    {
       ++input;
    }
};

int main()
{
    List l;
    for (unsigned int i= 0; i <= LIST_SIZE; i++)
    {
        unsigned int x = rand() % 100;
        l.push_back(x);
    }
    l.print_list(); // pre incremented
    //for_each(l.begin(),l.end(),Increment());
    for_each(l.begin(),l.end(),std::mem_fun_ref(&List::increment));
    l.print_list(); // incremented
    return 1;
}

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

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

发布评论

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

评论(2

长亭外,古道边 2024-11-06 23:41:36

mem_fun_reffor_each 结合使用,希望拥有每个单独项的成员函数,而不是容器类本身的成员函数。

为什么需要increment才能成为List的成员?它不以其状态运行。我想说作为一个自由函子完全没问题。

mem_fun_ref in conjunction with for_each wants to have a member function of each individual item, not of the container class itself.

Why do you need increment to be a member of List? It doesn't operate on its state. I'd say it's perfectly fine as a free-functor.

一身骄傲 2024-11-06 23:41:36
static void increment(unsigned int &input)
{
    ++input;
}

int main()
{
    List l;
    for (unsigned int i= 0; i <= LIST_SIZE; i++)
    {
        unsigned int x = rand() % 100;
        l.push_back(x);
    }
    l.print_list(); // pre incremented
    for_each(l.begin(),l.end(),std::ptr_fun(&increment));
    l.print_list(); // incremented
    return 1;
}
static void increment(unsigned int &input)
{
    ++input;
}

int main()
{
    List l;
    for (unsigned int i= 0; i <= LIST_SIZE; i++)
    {
        unsigned int x = rand() % 100;
        l.push_back(x);
    }
    l.print_list(); // pre incremented
    for_each(l.begin(),l.end(),std::ptr_fun(&increment));
    l.print_list(); // incremented
    return 1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文