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,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 { }};}
当我编译此代码时,我收到以下错误:
In file included from BPCFGParser.cpp:3,from experiments.cpp:2:BPCFGParser.h:408: error: specialization of ‘std::hash<BPCFGParser::ActiveEquivClass>’ after instantiationBPCFGParser.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 instantiationBPCFGParser.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>’
现在我必须为这些类专门化std :: hash(因为标准的std :: hash定义不包括用户定义的类型).当我在BPCFGParser类的定义之前移动这些模板特化时,我会尝试各种各样的错误,并在某处(http://www.parashift.com/c++-faq-lite/misc-technical-issues.html)我读到:
Whenever you use a class as a template parameter,the declaration of that class must be complete and not simply forward declared.
所以我被卡住了.我不能在BPCFGParser定义之后专门化模板,我不能在BPCFGParser定义之前专门化它们,我怎么能得到这个呢?
You need to move the specialization into an inner class insIDe of BPCFGParser. Doing so meets both requirements.
非常感谢你的答案:)
哈希类在命名空间std中定义.它不允许我在非命名空间范围内专门化哈希模板.甚至如下:
template <> class std::hash<ActiveEquivClass> {...
不工作.但是当我用命名空间std {}括起特化时,它会给出奇怪的错误:
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 inputBPCFGParser.h:222: error: expected unqualifIEd-ID at end of input
在velocityreviews给出的答案中,有人声称命名空间不能在类中定义.所以我仍然被困住了.
解决方法 您需要将专门化移动到BPCFGParser内部的内部类中.这样做符合这两个要求>专业化是在ActiveEquivClass的完整定义之后
>在使用专业化之前
例:
class BPCFGParser { class ActiveEquivClass { ... }; template <> class hash<ActiveEquivClass> { public: size_t operator()(const BPCFGParser::ActiveEquivClass & aec) const { } }; ... unordered_map<ActiveEquivClass,EqActiveEquivClass> discovered_active_edges;};总结
以上是内存溢出为你收集整理的c – 实例化后模板的专业化?全部内容,希望文章能够帮你解决c – 实例化后模板的专业化?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)