“'之前的预期初始化程序'<'令牌“在头文件中

时间:2010-05-07 17:59:21

标签: c++ boost header typedef

我对编程很新,而且通常会被头文件和包含内容搞糊涂了。我想帮助解决立即编译问题,并希望能够提供更清晰,更安全,更流畅的编写代码的方法。

我目前正在将许多曾经在main()中的代码重新打包到Simulation类中。我在这个类的头文件中遇到编译错误。我正在使用gcc版本4.2.1进行编译。

 // Simulation.h
#ifndef SIMULATION_H
#define SIMULATION_H

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <string>
#include <fstream>
#include <set>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>
#include <boost/multi_index/mem_fun.hpp>
#include <boost/multi_index/composite_key.hpp> 
#include <boost/shared_ptr.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <boost/tuple/tuple_io.hpp>

#include "Parameters.h"
#include "Host.h"
#include "rng.h"
#include "Event.h"
#include "Rdraws.h"

typedef multi_index_container< // line 33 - first error
  boost::shared_ptr< Host >,
  indexed_by< 
    hashed_unique< const_mem_fun<Host,int,&Host::getID> >, // 0 - ID index
    ordered_non_unique< tag<age>,const_mem_fun<Host,int,&Host::getAgeInY> >, // 1 - Age index
    hashed_non_unique< tag<household>,const_mem_fun<Host,int,&Host::getHousehold> >, // 2 - Household index
    ordered_non_unique< // 3 - Eligible by age & household
      tag<aeh>,
      composite_key<
    Host,
    const_mem_fun<Host,int,&Host::getAgeInY>,
    const_mem_fun<Host,bool,&Host::isEligible>,
    const_mem_fun<Host,int,&Host::getHousehold>
    >
      >,
    ordered_non_unique< // 4 - Eligible by household (all single adults)
      tag<eh>,
      composite_key<
    Host,
    const_mem_fun<Host,bool,&Host::isEligible>,
    const_mem_fun<Host,int,&Host::getHousehold>
    >
      >,
    ordered_non_unique< // 5 - Household & age
      tag<ah>,
      composite_key<
    Host,
    const_mem_fun<Host,int,&Host::getHousehold>,
    const_mem_fun<Host,int,&Host::getAgeInY>
    >
       >
    > // end indexed_by
  > HostContainer; 

typedef std::set<int> HHSet;

class Simulation
{
  public:
  Simulation( int sid );
  ~Simulation();

  // MEMBER FUNCTION PROTOTYPES
  void runDemSim( void );
  void runEpidSim( void );
  void ageHost( int id );
  int calcPartnerAge( int a );
  void executeEvent( Event & te );
  void killHost( int id );
  void pairHost( int id );
  void partner2Hosts( int id1, int id2 );
  void fledgeHost( int id );
  void birthHost( int id );
  void calcSI( void );
  double beta_ij_h( int ai, int aj, int s );
  double beta_ij_nh( int ai, int aj, int s );

 private:
  // SIMULATION OBJECTS
  double t;
  double outputStrobe;
  int idCtr;
  int hholdCtr;
  int simID;
  RNG rgen;
  HostContainer allHosts; // shared_ptr to Hosts - line 102 - second error
  HHSet allHouseholds; 
  int numInfecteds[ INIT_NUM_AGE_CATS ][ INIT_NUM_STYPES ]; 
  EventPQ currentEvents; 

  // STREAM MANAGEMENT
  void writeOutput();
  void initOutput();
  void closeOutput();

  std::ofstream ageDistStream;
  std::ofstream ageDistTStream;
  std::ofstream hhDistStream;
  std::ofstream hhDistTStream;

  std::string ageDistFile;
  std::string ageDistTFile;
  std::string hhDistFile; 
  std::string hhDistTFile;
};

#endif

我希望其他文件与此问题不太相关。当我用

编译时
g++ -g -o -c a.out -I /Applications/boost_1_42_0/ Host.cpp Simulation.cpp rng.cpp main.cpp Rdraws.cpp 

我得到了

Simulation.h:33: error: expected initializer before '<' token
Simulation.h:102: error: 'HostContainer' does not name a type

然后是一堆与无法识别HostContainer相关的其他错误。

似乎我对HostContainer的所有正确的Boost #includes都有所了解。还有什么可能出错?

我将非常感谢有关我的代码的即时建议,疑难解答提示和其他建议。我的计划是创建一个“HostContainer.h”文件,其中包含定义其标记的typedef和结构,类似于我在EventPQ容器的“Event.h”中所做的。我认为这是合法且良好的形式。

2 个答案:

答案 0 :(得分:4)

为了扩展我的评论,以下是如何解决这个问题,使其(a)可读,(b)可维护,(c)更容易调试。基本上,您为每个索引类型创建typedef,然后在容器定义中使用它们:

using namespace boost;
using namespace boost::multi_index;

typedef hashed_unique< 
            const_mem_fun<Host,int,&Host::getID> 
        > IDIndex;

typedef ordered_non_unique< 
            tag<age>,
            const_mem_fun<Host,int,&Host::getAgeInY> 
        > AgeIndex;

typedef hashed_non_unique< 
            tag<household>,
            const_mem_fun<Host,int,&Host::getHousehold> 
        > HouseholdIndex;

typedef ordered_non_unique<
            tag<aeh>,
            composite_key<
                Host,
                const_mem_fun<Host,int,&Host::getAgeInY>,
                const_mem_fun<Host,bool,&Host::isEligible>,
                const_mem_fun<Host,int,&Host::getHousehold>
            >
        > EligibilityByAgeAndHouseholdIndex;

typedef ordered_non_unique<
            tag<eh>,
            composite_key<
                Host,
                const_mem_fun<Host,bool,&Host::isEligible>,
                const_mem_fun<Host,int,&Host::getHousehold>
            >
        > EligibilityByHouseholdIndex;

typedef ordered_non_unique<
            tag<ah>,
            composite_key<
                Host,
                const_mem_fun<Host,int,&Host::getHousehold>,
                const_mem_fun<Host,int,&Host::getAgeInY>
            >
        > HouseholdAndAgeIndex;

typedef multi_index_container<
    boost::shared_ptr<Host>,
    indexed_by< 
        IDIndex,
        AgeIndex,
        HouseholdIndex,
        EligibilityByAgeAndHouseholdIndex,
        EligibilityByHouseholdIndex,
        HouseholdAndAgeIndex
    >
> HostContainer; 

它是自我记录的,因为索引名称是typedefed,因此您不需要注释来描述索引的用途。由于容器定义很短,因此您也可以省略“end indexed_by”类型的注释。

我不建议实际使用using指令;我只是把它放在那里,所以它会编译而不会过多地改变你的代码。

答案 1 :(得分:3)

multi_index_container似乎位于命名空间boost中。因此,您必须使用boost::multi_index_container明确引用它,或使用using声明/指令。

HostContainer错误是由第一个错误引起的。通常,您应该按顺序解决C ++编译错误。