Boost Multiindex:示例在cpp文件中编译但不在头文件中编译

时间:2014-06-27 22:15:30

标签: c++ boost multi-index

我正在尝试编译boost Multiindex example

我有一个由多个头文件和源文件组成的项目。 当我将以下代码放入某个源文件时,它运行良好,但是当我将此代码放入头文件时,它会给我下面的错误。 header和cpp都包含所需的boost头文件,否则boost工作正常。

我从未遇到过这样的问题而且我很困惑,原因可能是什么。

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

在依赖类型名称'employee_set :: nth_index'之前缺少'typename'          const employee_set :: nth_index&lt; 1&gt; :: type&amp; name_index = es.get&LT 1为卤素;();

预期不合格的身份证明          const employee_set :: nth_index&lt; 1&gt; :: type&amp; name_index = es.get&LT 1为卤素;();

2 个答案:

答案 0 :(得分:1)

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

nth_index&lt; 1&gt; :: type是一种称为依赖类型的东西。但编译器不知道它是一种值还是其他什么。写typename告诉他它确实是一种类型。

答案 1 :(得分:0)

从C ++编译器的角度来看,代码是位于.hpp还是.cpp文件中并不重要:头文件(通常)不是自己编译的,而是作为他们.cpp进入的#include文件的一部分进行处理。

因此,您的问题必须与在#34;标题&#34;之间移动代码时无意中应用的某些更改有关。和&#34;来源&#34;文件。看起来,

void print_out_by_name(const employee_set& es);

不是模板函数或类模板的一部分,因此它不能处理依赖类型或可能需要插入typename s的任何内容。也许这是一个较大的类的一部分,其中employee_set实际上是一个模板参数?