![]() |
| | #1 |
| Registered User Join Date: May 2009
Posts: 1
| forward declaration in C++ Code: class childClass
{
parentClass parent; //reference to class that created this child
childClass(parentClass p)
{
parent = p;
}
};
class parentClass
{
childClass child(this);
};
This would work fine in Java, which I'm more accustomed to, but I need to do it in C++. The problem being that C++ compilers read classes sequentially and this code gives the error "error C2061: syntax error : identifier 'parentClass'" because it does not know what parentClass is yet. All the advice on the internet says to create a forward declaration for parentClass BEFORE the definition for childClass, and then keeping the definition for parentClass after childClass, such: Code: class parentClass;
class childClass
{
parentClass parent; //reference to class that created this child
childClass(parentClass p)
{
parent = p;
}
};
class parentClass
{
childClass child(this);
};
arent' uses undefined class 'parentClass'" I've tried swapping around the classes, too, so that parentClass comes before childClass with a forward declaration for childClass. This gives the same error. I've also tried putting the classes in seperate *.cpp files. I must admit I don't really know how to do this, but the way I did it resulted in the same error (...uses undefined class...). What do you think? I'm a decent Java and C# programmer but C++ is just taking the ......... |
| ducttape is offline | |
| | #2 |
| Registered User Join Date: May 2007
Posts: 147
| If you take the function body out of the class, and move it into a CPP (or at least after the complete declaration of all the other classes ) you won't have a problem. |
| JVene is offline | |
| | #3 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,122
| You need to change this member variable: Code: parentClass parent; Code: parentClass* parent;
__________________ "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008 |
| cpjust is offline | |
| | #4 | |
| The larch Join Date: May 2006
Posts: 3,086
| Code: parentClass parent; //reference to class that created this child Use a pointer as mentioned above and the forward declaration will be enough.
__________________ I might be wrong. Quote:
| |
| anon is offline | |
| | #5 |
| Making mistakes Join Date: Dec 2008
Posts: 347
| You should use pointers or references anyway. Copying a whole class instance in a constructor? Ouch. |
| Brafil is offline | |
| | #6 | |
| Registered User Join Date: Jul 2009
Posts: 1
| Quote:
u r write in the internet most of the even dont understand the basics of c++.well in the forward declration is an unfilled class so u cant create an object but you can create an object pointer or references. | |
| sarav957 is offline | |
| | #7 |
| and the hat of sweating Join Date: Aug 2007 Location: Toronto, ON
Posts: 3,122
| You might want to check the expiration date on threads before posting. Five month old threads are pretty stale. Learning to type properly would help too.
__________________ "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008 |
| cpjust is offline | |
![]() |
| Tags |
| c++, forward declaration, uses undefined class |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| how do you resolve this error? | -EquinoX- | C Programming | 32 | 11-05-2008 04:35 PM |
| Quantum Random Bit Generator | shawnt | C++ Programming | 62 | 06-18-2008 10:17 AM |
| failure to import external C libraries in C++ project | nocturna_gr | C++ Programming | 3 | 12-02-2007 03:49 PM |
| We Got _DEBUG Errors | Tonto | Windows Programming | 5 | 12-22-2006 05:45 PM |
| Dikumud | maxorator | C++ Programming | 1 | 10-01-2005 06:39 AM |