My complete code is too long, but here is a snippet that will reflect the essence of my problem:
class BPCFGParser { public: ... ... class Edge { ... ... }; class ActiveEquivClass { ... ... }; class PassiveEquivClass { ... ... }; struct EqActiveEquivClass { ... ... }; struct EqPassiveEquivClass { ... ... }; unordered_map<ActiveEquivClass, Edge *, hash<ActiveEquivClass>, EqActiveEquivClass> discovered_active_edges; unordered_map<PassiveEquivClass, Edge *, hash<PassiveEquivClass>, EqPassiveEquivClass> discovered_passive_edges; }; namespace std { template <> class hash<BPCFGParser::ActiveEquivClass> { public: size_t operator()(const BPCFGParser::ActiveEquivClass & aec) const { } }; template <> class hash<BPCFGParser::PassiveEquivClass> { public: size_t operator()(const BPCFGParser::PassiveEquivClass & pec) const { } }; }
When I compile this code, I get the following errors:
In file included from BPCFGParser.cpp:3, from experiments.cpp:2: BPCFGParser.h:408: error: specialization of 'std::hash<BPCFGParser::ActiveEquivClass>' after instantiation BPCFGParser.h:408: error: redefinition of 'class std::hash<BPCFGParser::ActiveEquivClass>' /usr/include/c++/4.3/tr1_impl/functional_hash.h:44: error: previous definition of 'class std::hash<BPCFGParser::ActiveEquivClass>' BPCFGParser.h:445: error: specialization of 'std::hash<BPCFGParser::PassiveEquivClass>' after instantiation BPCFGParser.h:445: error: redefinition of 'class std::hash<BPCFGParser::PassiveEquivClass>' /usr/include/c++/4.3/tr1_impl/functional_hash.h:44: error: previous definition of 'class std::hash<BPCFGParser::PassiveEquivClass>'
Now I need to specialize std :: hash for these classes (since the standard definition of std :: hash does not include user-defined types). When I move on to these template specializations before defining the BPCFGParser class, I get a lot of errors for a lot of different things, and somewhere ( http://www.parashift.com/c++-faq-lite/misc-technical-issues.html ) I read that:
Whenever you use a class as a template parameter, the declaration of this class should be complete, and not just declared forward.
So I'm stuck. I cannot specialize templates after defining BPCFGParser , I cannot specify them before defining BPCFGParser , how can I make this work?
You need to move the specialization to the inner class inside BPCFGParser. This meets both requirements.
Thank you very much for your reply:)
hash class is defined in the std . This does not allow me to specialize templates for hash in a domain without a namespace. Even the following:
template <> class std::hash<ActiveEquivClass> { ...
does not work. However, when I conclude the specialization with namespace std {} , this gives a strange error:
In file included from BPCFGParser.cpp:3, from experiments.cpp:2: BPCFGParser.h:225: error: expected unqualified-id before 'namespace' experiments.cpp:7: error: expected `}' at end of input BPCFGParser.h:222: error: expected unqualified-id at end of input
In the answer given in velocityreviews , someone claims that namespaces cannot be defined inside classes. So I'm still stuck.