Say I got this C++ code:
class class1{
class2 *x;
}
class class2{
class1 *x;
}
The compiler would give an error in line 2 because it couldn't find class2, and the same if i switched the order of the classes. How do I solve this?
Say I got this C++ code:
class class1{
class2 *x;
}
class class2{
class1 *x;
}
The compiler would give an error in line 2 because it couldn't find class2, and the same if i switched the order of the classes. How do I solve this?
Declare class2 first:
Refer Forward declarations
Two things - one, you need semicolons after class declarations:
Two, you can create a declaration in front of the definitions of the classes. That tells the compiler that this class exists, and you have yet to define it. In this case, put a
class2
declaration in front of the definition ofclass1
: