Thread: forward declaration in C++

  1. #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 .........

  2. #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.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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.

    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).

  5. #5
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    You should use pointers or references anyway. Copying a whole class instance in a constructor? Ouch.

  6. #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.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    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

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 04:35 PM
  2. Quantum Random Bit Generator
    By shawnt in forum C++ Programming
    Replies: 62
    Last Post: 06-18-2008, 10:17 AM
  3. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

Tags for this Thread