C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 05-08-2009, 05:03 AM   #1
Registered User
 
Join Date: May 2009
Posts: 1
forward declaration in C++

myProgram.cpp:

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);
};
But this code then gives the error: "error C2079: 'myProgram: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   Reply With Quote
Old 05-08-2009, 05:15 AM   #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   Reply With Quote
Old 05-08-2009, 06:39 AM   #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;
to
Code:
parentClass* parent;
because a forward declaration doesn't tell the compiler anything about the size of the parentClass object. So you're stuck with using a pointer instead.
__________________
"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   Reply With Quote
Old 05-08-2009, 08:20 AM   #4
The larch
 
Join Date: May 2006
Posts: 3,086
Code:
parentClass parent;  //reference to class that created this child
The comment is wrong. This is not a reference and parent won't have anything to do with the (class) instance that created this child (in C++).

Use a pointer as mentioned above and the forward declaration will be enough.
__________________
I might be wrong.

Quote:
Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Quoted more than 1000 times (I hope).
anon is offline   Reply With Quote
Old 05-08-2009, 09:12 AM   #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   Reply With Quote
Old 10-10-2009, 11:56 AM   #6
Registered User
 
Join Date: Jul 2009
Posts: 1
Quote:
Originally Posted by Brafil View Post
You should use pointers or references anyway. Copying a whole class instance in a constructor? Ouch.
HI,
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   Reply With Quote
Old 10-10-2009, 03:00 PM   #7
and the hat of sweating
 
Join Date: Aug 2007
Location: Toronto, ON
Posts: 3,122
Quote:
Originally Posted by sarav957 View Post
HI,
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.
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   Reply With Quote
Reply

Tags
c++, forward declaration, uses undefined class

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:21 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22