2010-05-07 7 views
3

저는 프로그래밍에 익숙하지 않고 일반적으로 헤더 파일에 혼란 스럽습니다. 나는 즉각적인 컴파일 문제에 도움을 청하고 나의 코드를 작성하기위한보다 깔끔하고 안전하며 매끄러운 방법에 대한 일반적인 제안을 고맙게 생각한다."헤더 파일의 '<'token"앞에 예상 된 이니셜 라이저

저는 현재 Simulation 클래스에 main()에 있던 많은 코드를 다시 패키지하고 있습니다. 이 클래스의 헤더 파일에서 컴파일 오류가 발생합니다. 나는 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을 인식하지 관련된 다른 오류의 무리를 얻을.

내가 이해할 수있는 모든 부스트 #includes가있는 것처럼 보입니다. 그 밖의 무엇이 잘못 될 수 있습니까?

즉각적인 제안, 문제 해결 정보 및 내 코드에 대한 기타 조언에 감사드립니다. 내 계획은 EventPQ 컨테이너에 대해 "Event.h"에서 수행하는 것과 비슷한 태그를 정의하는 typedef와 struct를 포함하는 "HostContainer.h"파일을 만드는 것입니다. 나는 이것이 합법적이고 좋은 형태라고 생각합니다.

+4

typedef를 심각하게 고려해야합니다. 작은 조각을 typedef 한 다음 그것을 사용하여 빌드하십시오. –

+0

당신이 어떻게 깨뜨릴 지 예를 들어 주시겠습니까? – Sarah

+0

예. 내가 코멘트로 게시하기에는 너무 길기 때문에 답으로 게시했습니다. –

답변

3

multi_index_containerboost 인 것처럼 보입니다. 따라서 명시 적으로 boost::multi_index_container을 사용하거나 using 선언/지시어를 사용해야합니다.

HostContainer 오류는 첫 번째 오류로 인해 발생합니다. 보통 C++ 컴파일 오류를 순서대로 처리해야합니다.

+1

Gaaaaaaaaaaaaaaaahhhhhhh. 네, 고마워요. 컨테이너 요소 "indexed_by", "hashed_unique"및 "const_mem_fun"에 문제가 발생했습니다. 이러한 문제에 대한 해결책은 http://lists.boost.org/Archives/boost/2008/09/142085.php – Sarah

4

내 의견을 확장하려면 다음과 같이 (a) 읽기 쉽고 (b) 유지 관리가 쉬우 며 (c) 디버그하기 쉽게 만들 수있는 방법이 있습니다. 기본적으로, 당신은, 인덱스 유형 각각에 대한 형식 정의를 만들 컨테이너 정의 사람들을 사용 인덱스 이름이 typedefed 때문에 당신이를 설명하는 주석을 필요가 없습니다

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 지시문을 사용하지 않는 것이 좋습니다. 나는 코드를 너무 많이 바꾸지 않고서 컴파일 할 수 있도록 거기에 넣었다.

+0

입니다. James 대단히 감사합니다. 그건 의미가 있습니다. 이 모든 것을 HostContainer를 정의하기위한 헤더 파일에만 가지고 있다면 "using"지시문에 문제가 있습니까? 읽는 것이 훨씬 간단 해집니다. – Sarah

+1

@Sarah : using 지시문은 사용하지 않아야합니다. 특히 헤더에는 사용하지 말아야합니다. 총알을 물고 이름을 알아 냈어. –

관련 문제