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?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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
class2declaration in front of the definition ofclass1: