I have two classes, A and B, which both need to be able to reference each other, as shown below.
// A.h
class B; // Forward declare B
template<>
struct std::hash<B>; // Forward declare hash template specialization for B
class A {
std::unordered_map<B, int> map;
};
// B.h
#include "A.h"
class B {
A object;
}
template<>
struct std::hash<B> {
size_t operator()(__in const B& toHash) const;
}
In this current state, it tells me error C2139: 'std::hash<B>': an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_empty'
If I don't forward declare the hash template specialization, it tells me that I can't declare it after it's already used.
I also tried moving the hash template specialization's definition to where I'm forward declaring it now, and that also gave me an error.
What can I do to cause this to compile and behave the way I've laid out here?
The only thing I can think of would be to redefine B to instead store something like a std::unique_ptr<A>
, forward declare A instead of B, and have A import B.h rather than B import A.h. But this feels like a weird solution that doesn't really address the problem of dependencies like this. Am I missing something?