【问题标题】:boost::lambda : _1 is not a class or namespaceboost::lambda : _1 不是类或命名空间
【发布时间】:2013-01-21 17:13:19
【问题描述】:

我想从具有此类字符串作为属性的类列表中填充一组字符串(公共 getter 可用)。

我想使用 lambda 表达式和 std::for_each 来实现。

我正在考虑类似的事情:

class Foo
{
    const std::string& getMe() const;
}

...
std::list<Foo> foos; // Let's image the list is not empty
std::set<std::string> strings; // The set to be filled

using namespace boost::lambda;
std::for_each(foos.begin(), foos.end(), bind(
    std::set<std::string>::insert, &strings, _1::getMe()));

但是,我在编译时收到此错误:

_1 不是类或命名空间

谢谢。

【问题讨论】:

  • 是的,你没有解释你有什么问题,如果有的话
  • @andy_t 只是想知道这是否可行,因为我没有直接插入 _1 占位符,而是在其上调用方法。
  • @AndyT :是的,我有一个编译错误(在问题中添加了它)。干杯。

标签: c++ boost stl lambda


【解决方案1】:

正确的做法是:

class Foo
{
public:
    const void* getMe() const
    {
        return this;
    }
};

int main()
{
    std::list<Foo> foos(10);
    std::set<const void*> addresses; // The set to be filled

    using boost::bind;
    std::for_each(foos.begin(), foos.end(), bind(
        &std::set<const void*>::insert, &addresses, bind(&Foo::getMe, _1)));

    return 0;
}

【讨论】:

  • 你确定这会起作用吗? (不编译给我)
  • 我不明白为什么这不起作用。在 VC++10 中测试过,boost 1.50
猜你喜欢
  • 1970-01-01
  • 2012-12-03
  • 2018-04-03
  • 1970-01-01
  • 1970-01-01
  • 2014-07-24
  • 2016-03-27
  • 1970-01-01
相关资源
最近更新 更多