2015-06-27 63 views
1

增加的typedef当我是新的C++和我正在学习从加速C++(与书的人,我想运行§7.4中描述的程序)错误头

程序我正在寻找使用一些typedefs - 我收集,如果我将这些添加到头文件,包含该头的任何源文件也将能够使用typedefs。

我的头是:

#ifndef READ_GRAMMAR_H_INCLUDED 
#define READ_GRAMMAR_H_INCLUDED 

typedef std::vector<std::string> Rule; 
typedef std::vector<Rule> Rule_collection; 
typedef std::map<std::string, Rule_collection> Grammar; 

Grammar read_grammar(std::istream& in); 

#endif // READ_GRAMMAR_H_INCLUDED 

这是给我的错误error: 'map' in namespace 'std' does not name a type

如果我改变第三的typedef typedef std::vector<Rule_collection> Grammar;(不,我想这一点,只是举例),它构建没有错误。

任何想法是什么问题?我不知道我是在做一些错误的小事,或者整个方法是不正确的

+2

你需要'#包括'''和''编辑:和''和 – AndyG

+1

''。 – chris

+0

@ rbennett485我相信地图是间谍! –

回答

3

它说它在命名空间std中找不到map。你需要包含它,以便编译器能够找到它。同样,你需要包括标头std::vectorstd::stringstd::istream

#ifndef READ_GRAMMAR_H_INCLUDED 
#define READ_GRAMMAR_H_INCLUDED 
#include <map> 
#include <vector> 
#include <string> 
#include <istream> 

typedef std::vector<std::string> Rule; 
typedef std::vector<Rule> Rule_collection; 
typedef std::map<std::string, Rule_collection> Grammar; 

Grammar read_grammar(std::istream& in); 

#endif // READ_GRAMMAR_H_INCLUDED 

如果你觉得勇敢,你可能也想阅读有关前置声明 - 它们的用途,优点和缺点,但我怀疑它需要这个特殊情况。

+1

这很有道理。任何想法,为什么它只是给失踪的'#包括'错误,虽然,而不是缺少'#包括'? – rbennett485

+0

你可能已经把它们包含在别的地方了,实际的错误是由包含这个头文件的'.cpp'文件抛出的。 –

+0

@ rbennett485,可能你在这个标题之前包含的东西包括其他东西。 – chris

3

你必须包含头文件,如果你没有包含它们,那么你的程序将如何使用它?

#ifndef READ_GRAMMAR_H_INCLUDED 
#define READ_GRAMMAR_H_INCLUDED 

#include <istream> 
#include <string> 
#include <vector> 
#include <map> 

typedef std::vector<std::string> Rule; 
typedef std::vector<Rule> Rule_collection; 
typedef std::map<std::string, Rule_collection> Grammar; 

Grammar read_grammar(std::istream& in); 

#endif // READ_GRAMMAR_H_INCLUDED