boost::lambda 表达式无法编译

发布于 2024-08-27 12:14:49 字数 2206 浏览 4 评论 0原文

我尝试编写一个函数,使用 boost lambda 库计算两个码字之间的汉明距离。我有以下代码:

#include <iostream>
#include <numeric>
#include <boost/function.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/if.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>

template<typename Container>
int hammingDistance(Container & a, Container & b) {
  return std::inner_product(
    a.begin(),
    a.end(),
    b.begin(),
    (_1 + _2),
    boost::lambda::if_then_else_return(_1 != _2, 1, 0)
  );
}

int main() {
  boost::array<int, 3> a = {1, 0, 1}, b = {0, 1, 1};
  std::cout << hammingDistance(a, b) << std::endl;
}

我得到的错误是:

HammingDistance.cpp: In function ‘int hammingDistance(Container&, Container&)’:
HammingDistance.cpp:15: error: no match for ‘operator+’ in ‘<unnamed>::_1 + <unnamed>::_2’
HammingDistance.cpp:17: error: no match for ‘operator!=’ in ‘<unnamed>::_1 != <unnamed>::_2’
/usr/include/c++/4.3/boost/function/function_base.hpp:757: note: candidates are: bool boost::operator!=(boost::detail::function::useless_clear_type*, const boost::function_base&)
/usr/include/c++/4.3/boost/function/function_base.hpp:745: note:                 bool boost::operator!=(const boost::function_base&, boost::detail::function::useless_clear_type*)

这是我第一次使用 boost lambda。请告诉我哪里出错了。谢谢。

编辑:

非常感谢大家!这是工作代码(仅供参考):

#include <iostream>
#include <numeric>
#include <boost/function.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/if.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/array.hpp>

using boost::lambda::_1;
using boost::lambda::_2;

template<typename Container>
int hammingDistance(Container & a, Container & b) {
  return std::inner_product(
    a.begin(),
    a.end(),
    b.begin(),
    0,
    (_1 + _2),
    boost::lambda::if_then_else_return(_1 != _2, 1, 0)
  );
}

int main() {
  boost::array<int, 3> a = {1, 0, 1}, b = {0, 1, 1};
  std::cout << hammingDistance(a, b) << std::endl;
}

I tried to write a function that calculates a hamming distance between two codewords using the boost lambda library. I have the following code:

#include <iostream>
#include <numeric>
#include <boost/function.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/if.hpp>
#include <boost/bind.hpp>
#include <boost/array.hpp>

template<typename Container>
int hammingDistance(Container & a, Container & b) {
  return std::inner_product(
    a.begin(),
    a.end(),
    b.begin(),
    (_1 + _2),
    boost::lambda::if_then_else_return(_1 != _2, 1, 0)
  );
}

int main() {
  boost::array<int, 3> a = {1, 0, 1}, b = {0, 1, 1};
  std::cout << hammingDistance(a, b) << std::endl;
}

And the error I am getting is:

HammingDistance.cpp: In function ‘int hammingDistance(Container&, Container&)’:
HammingDistance.cpp:15: error: no match for ‘operator+’ in ‘<unnamed>::_1 + <unnamed>::_2’
HammingDistance.cpp:17: error: no match for ‘operator!=’ in ‘<unnamed>::_1 != <unnamed>::_2’
/usr/include/c++/4.3/boost/function/function_base.hpp:757: note: candidates are: bool boost::operator!=(boost::detail::function::useless_clear_type*, const boost::function_base&)
/usr/include/c++/4.3/boost/function/function_base.hpp:745: note:                 bool boost::operator!=(const boost::function_base&, boost::detail::function::useless_clear_type*)

This is the first time I am playing with boost lambda. Please tell me where I am going wrong. Thanks.

EDIT:

Thanks a lot guys! Here is the working code (just for reference):

#include <iostream>
#include <numeric>
#include <boost/function.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/if.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/array.hpp>

using boost::lambda::_1;
using boost::lambda::_2;

template<typename Container>
int hammingDistance(Container & a, Container & b) {
  return std::inner_product(
    a.begin(),
    a.end(),
    b.begin(),
    0,
    (_1 + _2),
    boost::lambda::if_then_else_return(_1 != _2, 1, 0)
  );
}

int main() {
  boost::array<int, 3> a = {1, 0, 1}, b = {0, 1, 1};
  std::cout << hammingDistance(a, b) << std::endl;
}

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

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

发布评论

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

评论(2

梦忆晨望 2024-09-03 12:14:49

第一个问题:使用 boost/lambda 时,包含 而不是 >

第二个问题:在 #includes

仍然无法编译


后,您需要一个 using namespace boost::lambda编辑:
第三个问题 - std::inner_product 需要 6 个参数,缺少初始化参数。可能添加 0 作为第四个参数。

First problem: when using boost/lambda, include <boost/lambda/bind.hpp> instead of <boost/bind.hpp>

Second problem: you need a using namespace boost::lambda after the #includes

still doesn't compile though


Edit:
Third problem - you need 6 arguments for std::inner_product, you're missing an initialization argument. probably add 0 as the forth argument.

悍妇囚夫 2024-09-03 12:14:49

我可能是错的,但我认为您应该在函数之前使用 using namespace boost::lambda; 因为占位符(_1、_2 等)位于该名称空间中。

I could be wrong but I think you should have using namespace boost::lambda; before your function as the placeholders (_1, _2 etc.) are in that namespace.

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