2017-10-19 10 views
0

juste essayer l'exemple boost::multi_index de base et la réception et l'erreur:Boost exemple multi_index: Erreur: opérandes invalides à l'expression binaire

#include <boost/multi_index_container.hpp> 
#include <boost/multi_index/ordered_index.hpp> 
#include <boost/multi_index/identity.hpp> 
#include <boost/multi_index/member.hpp> 

using namespace boost::multi_index; 

struct employee 
{ 
    int id; 
    std::string name; 

    employee(int id, const std::string &name) : id(id), name(name) {} 

    bool operator<(const employee &e) const { return id < e.id; } 
}; 

// define a multiply indexed set with indices by id and name 
typedef multi_index_container< 
    employee, 
    indexed_by< 
     // sort by employee::operator< 
     ordered_unique<identity<employee> >, 

     // sort by less<string> on name 
     ordered_non_unique<member<employee, std::string, &employee::name> > > > 
    employee_set; 

void print_out_by_name(const employee_set &es) 
{ 
    // get a view to index #1 (name) 
    const employee_set::nth_index<1>::type &name_index = es.get<1>(); 
    // use name_index as a regular std::set 
    std::copy(
     name_index.begin(), name_index.end(), 
     std::ostream_iterator<employee>(std::cout)); // Looks like the problem is here. 
} 

Obtenir cette erreur:

/Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iterator:813:28: error: invalid operands to binary expression ('ostream_type' (aka 
      'basic_ostream<char, std::__1::char_traits<char> >') and 'const employee') 
       *__out_stream_ << __value_; 
+1

Le [exemple complet] (http://www.boost.org/doc/libs/release/libs/mul ti_index/example/basic.cpp) implémente 'operator <<' pour 'employee'. – Praetorian

+0

@Praetorian n'a pas vu ça. Je vais les vérifier. Merci – BAR

Répondre

1

Il semble une mise en œuvre est nécessaire :

std::ostream &operator<<(std::ostream &os, const employee &obj) 
{ 
    os << obj.id << "/" << obj.name; 
    return os; 
}