0

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>();

user695652
  • 4,105
  • 7
  • 40
  • 58

2 Answers2

1

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.

burner
  • 323
  • 2
  • 10
  • Thats what I thought too at first, but when I do this I get the error: error: use 'template' keyword to treat 'nth_index' as a dependent template name const typename employee_set::nth_index<1>::type& name_index=es.get<1>(); Also isn't it weird that it worked in the cpp file but not in the h file? – user695652 Jun 27 '14 at 23:47
  • very strange indent, I never got that error msg. Can you use c++11? if so. const auto& is your friend – burner Jun 28 '14 at 20:29
  • thanks for the suggestions, but unfortunately, I can't use c++11 – user695652 Jun 28 '14 at 21:48
0

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?

Joaquín M López Muñoz
  • 5,243
  • 1
  • 15
  • 20
  • I typedefed employee_set in A.h and print_out_by_name is a function of class A. – user695652 Jun 29 '14 at 21:21
  • 1
    Do you mean `print_out_by_name` is a member function of class `A`? Is class `A` a class template? If so, try const typename employee_set::template nth_index<1>::type& name_index=es.template get<1>(); – Joaquín M López Muñoz Jun 30 '14 at 06:19
  • Nice, the es.template get<1>() worked; why is that? Btw: Yes, I meant that print_out_by_name is a member funciton of A. – user695652 Jun 30 '14 at 18:29
  • Check for instance http://stackoverflow.com/questions/610245/where-and-why-do-i-have-to-put-the-template-and-typename-keywords . Note that your original problem does not depend on whether the offending code lives in a header or a .cpp file. – Joaquín M López Muñoz Jun 30 '14 at 20:03