2010-04-01 13 views
4

J'ai essayé d'écrire une fonction qui calcule une distance de hamming entre deux mots de code en utilisant la bibliothèque boost lambda. J'ai le code suivant:boost :: l'expression lambda ne compile pas

#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; 
} 

Et l'erreur que je reçois est:

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*) 

C'est la première fois que je joue avec boost lambda. S'il vous plaît, dites-moi où je me trompe. Merci.

EDIT:

Merci un gars du lot! Voici le code de travail (juste pour la référence):

#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; 
} 
+2

Vous devriez appeler 'inner_product' comme' std :: inner_product (a.begin(), a.end(), b.begin(), 0, ...); '. – kennytm

Répondre

5

Premier problème: lors de l'utilisation boost/lambda, comprennent <boost/lambda/bind.hpp> au lieu de <boost/bind.hpp>

Deuxième problème: vous avez besoin d'un using namespace boost::lambda après les #includes

ne compile pas encore si


Edit:
Troisième problème - vous avez besoin de 6 arguments pour std::inner_product, il vous manque un argument d'initialisation. probablement ajouter 0 comme le quatrième argument.

+0

Merci beaucoup! Ça a marché! –

1

Je peux me tromper, mais je pense que vous devriez avoir using namespace boost::lambda; avant votre fonction que les espaces réservés (_1, _2, etc.) sont dans cet espace.