Reputation: 2163
I'm new to C++, and I'm learning from Accelerated C++ (for anyone with the book, I'm trying to run the program described in §7.4)
The program I'm looking at uses some typedefs - I gather that if I add these to a header file, any source file which includes that header will be able to use the typedefs too.
My header is:
#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
This is giving me the error error: 'map' in namespace 'std' does not name a type
If I change the third typedef to typedef std::vector<Rule_collection> Grammar;
(not that I want this, just for example) it builds with no errors.
Any idea what the problem is? I have no idea whether I'm doing something trivial the wrong way, or whether the whole approach is incorrect
Upvotes: 1
Views: 670
Reputation: 167
You must include the header files, if you don't have them included then how is your program going to use it?
#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
Upvotes: 3
Reputation: 14831
It says it cannot find map
in namespace std
. You need to include it so that the compiler can find it. Similarly, you'll need to include headers for std::vector
std::string
and std::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
If you feel courageous, you might also want to read about forward declarations - their usage, pros and cons, but I doubt it's needed in this particular case.
Upvotes: 3