I have two C++ classes/structs which use each other's type alias, but the compiler doesn't recognize one of the type aliases.
In "A.h":
#pragma once
#include "B.h"
struct A {
using id_t = int;
B::id_t id;
};
And in "B.h":
#pragma once
#include "A.h"
struct B {
using id_t = int;
A::id_t id;
};
In "main.cpp":
#include "A.h"
#include "B.h"
int main(int argc, char const *argv[])
{
A a;
return 0;
}
When I compiled the code, I got an error message as
In file included from ../A.h:3,
from ../main.cc:1:
../B.h:7:5: error: ‘A’ does not name a type
7 | A::id_t id;
| ^
I know that C++ forbids classes containing a member field of each other, which causes an "infinite loop", but why isn't it allowed to use each other's type alias?
Using forward declaration also seems infeasible, because the classes must know each other's "complete" definition.