私の完全なコードは長すぎますが、ここに私の問題の本質を反映するスニペットがあります:
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 {
}
};
}
このコードをコンパイルすると、次のエラーが発生します。
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>’
ここで、これらのクラス用に std::hash を特殊化する必要があります (標準の std::hash 定義にはユーザー定義型が含まれていないため)。これらのテンプレートの特殊化を class の定義の前に移動するとBPCFGParser
、さまざまなことを試みたときにさまざまなエラーが発生し、どこか ( http://www.parashift.com/c++-faq-lite/misc-technical-issues .html ) 私はそれを読みました:
クラスをテンプレート パラメーターとして使用する場合は常に、そのクラスの宣言を完全にする必要があり、単純に前方宣言するのではありません。
だから私は立ち往生しています。BPCFGParser
定義後にテンプレートを特殊化することはできません。また、定義前にテンプレートを特殊化することもできませんBPCFGParser
。どうすればこれを機能させることができますか?
特殊化を BPCFGParser 内の内部クラスに移動する必要があります。そうすることで、両方の要件が満たされます。
答えてくれてどうもありがとう:)
hash
class は namespace 内で定義されますstd
。hash
名前空間以外のスコープでテンプレートを特殊化することはできません。以下でも:
template <>
class std::hash<ActiveEquivClass> {
...
動作しませんでした。ただし、特殊化を で囲むとnamespace 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 input
BPCFGParser.h:222: error: expected unqualified-id at end of input
velocityreviewsで与えられた回答で、誰かが名前空間をクラス内で定義できないと主張しています。だから私はまだ立ち往生しています。