I have a circular dependency created by a nested enum.
class A
{
enum class A_enum {};
void method_which_uses_class_B() {}
}
class B
{
void method_which_uses_A_enum() {}
}
That gives you an idea of the overall structure.
Specifically, I have the enum class defined in this way:
... file containing class A
class A
{
enum class A_enum
{
ITEM_A, ITEM_B
};
// then the rest of A goes here
}
// in my main code file
#include "class B file"
#include "class A file"
... code
The solution therefore would be to move the enum class out of class A and put code/files/include's in the correct order to prevent the problem.
But it's rather nice to have the enum class nested inside class A for my purposes. Is there a way of pre-defining that there will be a class A and what the enum inside that class will be? I tried the following, but it didn't work because I was defining the class twice:
class A;
enum class A::A_enum
{
contenta, contentb
};
or
class A
{
enum class A_enum {...};
}
then
class A
{
// the rest of it
// obviously doesn't work because already defined class A
}
The only way to handle this situation is to implement A::method_which_uses_class_B() after the definition of class B.
class A
{
enum class A_enum {};
void method_which_uses_class_B();
};
class B
{
void method_which_uses_A_enum() {}
};
void A::method_which_uses_class_B()
{
}
Forward Declarations can solve this problem:
class A;
class B;
class A {
public:
enum class A_enum {};
void method_which_uses_class_B();
};
class B {
public:
void method_which_uses_A_enum();
};
void A::method_which_uses_class_B() {
A::A_enum instance_of_a_enum;
A instance_of_a;
B instance_of_b;
}
void B::method_which_uses_A_enum() {
A::A_enum instance_of_a_enum;
A instance_of_a;
B instance_of_b;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With