user695652
user695652

Reputation: 4275

Boost Multiindex: Example compiles in cpp files but not in header

I am trying to compile the boost Multiindex example

I have a project consisting of multiple header and source files. When I put the following code into some source file it works well, but it would give me the errors below, when I this code into a header file. Both header and cpp include the required boost header files and boost works fine otherwise.

I have never encountered such a problem and I am quite confused as what the reason could be.

// 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;

where employee is a simple struct.

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
    }

missing 'typename' prior to dependent type name 'employee_set::nth_index' const employee_set::nth_index<1>::type& name_index=es.get<1>();

expected unqualified-id const employee_set::nth_index<1>::type& name_index=es.get<1>();

Upvotes: 0

Views: 167

Answers (2)

From the point of view of the C++ compiler, it is immaterial whether the code is located in a .hpp or in a .cpp file: header files are (usually) not compiled on their own, but rather processed as part of the .cpp files they're #included into.

So, your problem must be related to some change you are inadvertently applying when moving code between "header" and "source" files. As it looks,

void print_out_by_name(const employee_set& es);

is not a template function or part of class template and hence it can't deal with dependent types or anything that might require the insertion of typenames. Maybe this is part of a larger class where employee_set is actually a template parameter?

Upvotes: 0

burner
burner

Reputation: 333

try

const typename employee_set::nth_index<1>::type& name_index=es.get<1>();

nth_index<1>::type is something called a depended type. But the compiler doesn't know whether it is a type of value or whatever. And writing typename tells him that it is indeed a type.

Upvotes: 1

Related Questions