相关疑难解决方法(0)

使用for_each调用成员函数

这是我的原始代码

#include "stdafx.h"
#include <string>
#include <list>
#include <algorithm>
using namespace std;


class testing
{
public:
    int value;
    testing(int v)
    {
        value = v;
    }

    int getval()
    {
        return(value);
    }

};

void func(testing& ob)
{
    printf("The value is %d\n", ob.value);
}

int _tmain(int argc, _TCHAR* argv[])
{
    std::list<testing> testvar[3];

    testing t1(0);
    testing t2(1);
    testing t3(3);

    testvar[0].push_back(t1);
    testvar[0].push_back(t2);
    testvar[0].push_back(t3);

    std::for_each(testvar[0].begin(), testvar[0].end(), func);

    printf("Reached End");
    getchar();
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我修改它使func成为一个成员函数,并得到奇怪的编译错误,我在网上搜索,有人告诉使用bind1st,bind2nd

#include "stdafx.h"
#include <string>
#include <list>
#include <algorithm>
using namespace std;

class …
Run Code Online (Sandbox Code Playgroud)

c++ foreach-loop-container

2
推荐指数
1
解决办法
9419
查看次数

标签 统计

c++ ×1

foreach-loop-container ×1