"Expected initializer to" <token "in the header file

I am new to programming and tend to confuse header files and includes. I would like to help with immediate compilation and would appreciate general suggestions for cleaner, safer, and smoother ways to write my code.

I am currently repackaging a lot of code that used to be in main () into a class Simulation. I get a compilation error with a header file for this class. I am compiling with gcc version 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

I hope other files are not so important for this problem. When I compile with

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

I get

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

and then many other errors related to not recognizing HostContainer.

, Boost #includes HostContainer, . ?

, . "HostContainer.h", typedef structs, , , "Event.h" EventPQ. , .

+3
2

multi_index_container , boost. boost::multi_index_container, using /.

HostContainer . ++ .

+3

, , (a) , (b) (c) . typedefs , , :

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; 

, , . , "end indexed_by".

using; , , .

+4

Source: https://habr.com/ru/post/1744540/


All Articles