C++ 头文件中的“'<'标记之前的预期初始化程序”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2790619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Expected initializer before '<' token" in header file
提问by Sarah
I'm pretty new to programming and am generally confused by header files and includes. I would like help with an immediate compile problem and would appreciate general suggestions about cleaner, safer, slicker ways to write my code.
我对编程很陌生,通常对头文件和包含感到困惑。我想立即解决编译问题,并希望得到有关编写代码的更干净、更安全、更灵活的方法的一般建议。
I'm currently repackaging a lot of code that used to be in main() into a Simulation
class. I'm getting a compile error with the header file for this class. I'm compiling with gcc version 4.2.1.
我目前正在将很多以前在 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
I'm hoping the other files aren't so relevant to 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 a bunch of other errors related to not recognizing the HostContainer.
然后是一堆与无法识别 HostContainer 相关的其他错误。
It seems like I have all the right Boost #includes for the HostContainer to be understood. What else could be going wrong?
似乎我有所有正确的 Boost #includes 来理解 HostContainer。还有什么问题?
I would appreciate immediate suggestions, troubleshooting tips, and other advice about my code. My plan is to create a "HostContainer.h" file that includes the typedef and structs that define its tags, similar to what I'm doing in "Event.h" for the EventPQ container. I'm assuming this is legal and good form.
我将不胜感激关于我的代码的即时建议、故障排除技巧和其他建议。我的计划是创建一个“HostContainer.h”文件,其中包含定义其标签的 typedef 和结构,类似于我在“Event.h”中为 EventPQ 容器所做的。我假设这是合法且良好的形式。
采纳答案by mkj
The multi_index_container
seems to be in namespace boost
. So you have to refer to it either explicitly with boost::multi_index_container
, or use using
declarations/directives.
在multi_index_container
似乎是在命名空间boost
。因此,您必须boost::multi_index_container
使用 或使用using
声明/指令显式引用它。
The HostContainer
error is caused by the first error. Usually you should address C++ compilation errors in order.
该HostContainer
错误是由第一个错误引起的。通常,您应该按顺序解决 C++ 编译错误。
回答by James McNellis
To expand on my comment, here's how you could break this up to make it (a) readable, (b) maintainable, and (c) easier to debug. Basically, you create typedefs for each of the index types, then use those in your container definition:
为了扩展我的评论,这里是如何分解它以使其 (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;
It is self-documenting because the index names are typedefed, so you don't need the comments to describe the purpose of the indices. Since the container definition is quite short, you can omit the "end indexed_by" type comments as well.
它是自文档化的,因为索引名称是类型定义的,因此您不需要注释来描述索引的用途。由于容器定义很短,您也可以省略“end indexed_by”类型的注释。
I don't recommend actually using the using directive; I just put it there so it would compile without me changing your code too much.
我不建议实际使用 using 指令;我只是把它放在那里,这样它就可以编译而无需我过多地更改您的代码。