circular dependencies, forward doesn't help.

Discussion in 'C++' started by rammel, Nov 24, 2006.

  1. rammel

    rammel Guest

    class1.h
    -----------
    #ifndef CLASS1_H_
    #define CLASS1_H_
    #include "class2.h" // inclusion
    class class2; // and forward

    class class1
    {
    private:
    class2 aRererence;
    // --- gcc says: "error: field 'aRererence' has incomplete type" ---
    public:
    class1() :aReference( class2& aR ) {aReference = aR;};
    // --- error according to the missing type of aReference ---
    };
    #endif /*CLASS1_H_*/

    ------------
    class2.h
    ------------
    #ifndef CLASS2_H_
    #define CLASS2_H_
    #include <vector>
    #include "class1.h" // inclusion
    class class1; // and forward
    class class2
    {
    private:
    std::vector<class1> someContainer;
    public:
    class2();
    };
    #endif /*CLASS2_H_*/
    -----------------------------------------

    What is the correct way around this error? I guess I could declare the
    field as pointer, but I wanted to use the c++ reference mechanism.
    Thanks for your help in advance.
     
    rammel, Nov 24, 2006
    #1
    1. Advertising

  2. rammel

    Hooyoo Guest

    rammel wrote:
    > class1.h
    > -----------
    > #ifndef CLASS1_H_
    > #define CLASS1_H_
    > #include "class2.h" // inclusion
    > class class2; // and forward
    >
    > class class1
    > {
    > private:
    > class2 aRererence;
    > // --- gcc says: "error: field 'aRererence' has incomplete type" ---
    > public:
    > class1() :aReference( class2& aR ) {aReference = aR;};
    > // --- error according to the missing type of aReference ---
    > };
    > #endif /*CLASS1_H_*/
    >
    > ------------
    > class2.h
    > ------------
    > #ifndef CLASS2_H_
    > #define CLASS2_H_
    > #include <vector>
    > #include "class1.h" // inclusion
    > class class1; // and forward
    > class class2
    > {
    > private:
    > std::vector<class1> someContainer;
    > public:
    > class2();
    > };
    > #endif /*CLASS2_H_*/
    > -----------------------------------------
    >
    > What is the correct way around this error? I guess I could declare the
    > field as pointer, but I wanted to use the c++ reference mechanism.
    > Thanks for your help in advance.


    The same question I asked before:
    "How to solve this problem?"
    http://groups.google.com/group/comp...3d770/5f1f69a762f5a2ac?hl=en#5f1f69a762f5a2ac
     
    Hooyoo, Nov 24, 2006
    #2
    1. Advertising

  3. rammel

    rammel Guest


    > The same question I asked before:
    > "How to solve this problem?"
    > http://groups.google.com/group/comp...3d770/5f1f69a762f5a2ac?hl=en#5f1f69a762f5a2ac


    Beeing a c++ newbie I have to admit that I was unable to get the point
    from that discussion. So could you please be so kind and alter the
    right line of my code?

    I've read about the rules when to use forward or inclusion and tried
    the most combinations without success.
    I think I can't put out one of the includes because class1 holds an
    vector of class2-elements and class2 needs a referece to the class1 it
    sticks in.
    I also have no clue how to redesign this without losing functionality.
     
    rammel, Nov 24, 2006
    #3
  4. rammel

    Hooyoo Guest

    rammel wrote:
    > class1.h
    > -----------
    > #ifndef CLASS1_H_
    > #define CLASS1_H_
    > #include "class2.h" // inclusion
    > class class2; // and forward
    >
    > class class1
    > {
    > private:
    > class2 aRererence;
    > // --- gcc says: "error: field 'aRererence' has incomplete type" ---
    > public:
    > class1() :aReference( class2& aR );

    Implement this method in your class1.cpp file, just declare it here.
    > // --- error according to the missing type of aReference ---
    > };
    > #endif /*CLASS1_H_*/
    >
    > ------------
    > class2.h
    > ------------
    > #ifndef CLASS2_H_
    > #define CLASS2_H_
    > #include <vector>
    > #include "class1.h" // inclusion
    > class class1; // and forward
    > class class2
    > {
    > private:
    > std::vector<class1> someContainer;
    > public:
    > class2();
    > };
    > #endif /*CLASS2_H_*/
    > -----------------------------------------
    >
    > What is the correct way around this error? I guess I could declare the
    > field as pointer, but I wanted to use the c++ reference mechanism.
    > Thanks for your help in advance.
     
    Hooyoo, Nov 24, 2006
    #4
  5. rammel

    rammel Guest

    Hooyoo wrote:
    > > class1() :aReference( class2& aR );

    > Implement this method in your class1.cpp file, just declare it here.
    > > // --- error according to the missing type of aReference ---



    thank you again but that didn't help.
    the main problem is already that declaration above:

    class2 aRererence; // compiler says: "incomplete type"
     
    rammel, Nov 24, 2006
    #5
  6. rammel wrote:

    > class1.h
    > -----------
    > #ifndef CLASS1_H_
    > #define CLASS1_H_
    > #include "class2.h" // inclusion
    > class class2; // and forward


    why do you need both of these?


    >
    > class class1
    > {
    > private:
    > class2 aRererence;
    > // --- gcc says: "error: field 'aRererence' has incomplete type" ---
    > public:
    > class1() :aReference( class2& aR ) {aReference = aR;};
    > // --- error according to the missing type of aReference ---
    > };
    > #endif /*CLASS1_H_*/
    >
    > ------------
    > class2.h
    > ------------
    > #ifndef CLASS2_H_
    > #define CLASS2_H_
    > #include <vector>
    > #include "class1.h" // inclusion
    > class class1; // and forward
    > class class2
    > {
    > private:
    > std::vector<class1> someContainer;
    > public:
    > class2();
    > };
    > #endif /*CLASS2_H_*/
    > -----------------------------------------
    >
    > What is the correct way around this error? I guess I could declare the
    > field as pointer, but I wanted to use the c++ reference mechanism.


    so declare it as a reference?

    class2& aRererence;

    Are you sure what you posted is your actual code?

    > Thanks for your help in advance.



    --
    Nick Keighley
     
    Nick Keighley, Nov 24, 2006
    #6
  7. Nick Keighley wrote:
    > rammel wrote:
    >
    > > class1.h
    > > -----------
    > > #ifndef CLASS1_H_
    > > #define CLASS1_H_
    > > #include "class2.h" // inclusion
    > > class class2; // and forward

    >
    > why do you need both of these?
    >
    >
    > >
    > > class class1
    > > {
    > > private:
    > > class2 aRererence;
    > > // --- gcc says: "error: field 'aRererence' has incomplete type" ---
    > > public:
    > > class1() :aReference( class2& aR ) {aReference = aR;};
    > > // --- error according to the missing type of aReference ---
    > > };
    > > #endif /*CLASS1_H_*/
    > >
    > > ------------
    > > class2.h
    > > ------------
    > > #ifndef CLASS2_H_
    > > #define CLASS2_H_
    > > #include <vector>
    > > #include "class1.h" // inclusion
    > > class class1; // and forward
    > > class class2
    > > {
    > > private:
    > > std::vector<class1> someContainer;
    > > public:
    > > class2();
    > > };
    > > #endif /*CLASS2_H_*/
    > > -----------------------------------------
    > >
    > > What is the correct way around this error? I guess I could declare the
    > > field as pointer, but I wanted to use the c++ reference mechanism.

    >
    > so declare it as a reference?
    >
    > class2& aRererence;
    >
    > Are you sure what you posted is your actual code?
    >
    > > Thanks for your help in advance.


    try this:-

    class1.h
    -----------
    #ifndef CLASS1_H_
    #define CLASS1_H_
    // #include "class2.h" *** REMOVED
    class class2;

    class class1
    {
    private:
    class2& aRererence; // reference
    public:
    class1(class2& aR):aReference(aR) {}; // corrected
    };


    #endif /*CLASS1_H_*/

    ------------
    class2.h
    ------------
    #ifndef CLASS2_H_
    #define CLASS2_H_
    #include <vector>
    #include "class1.h" // inclusion
    // removed unnec. forward ref
    class class2
    {
    private:
    std::vector<class1> someContainer;
    public:
    class2();
    };
    #endif /*CLASS2_H_*/


    I've not compiled this


    --
    Nick Keighley
     
    Nick Keighley, Nov 24, 2006
    #7
  8. rammel

    rammel Guest

    i totally missed to use the & operator while declaring that field, but
    declearing as reference was exactly what I wanted to do. thank you very
    much!

    (the constructor was false too but just a typo, that wasn't my actual
    code, just an abstracted example)
     
    rammel, Nov 24, 2006
    #8
  9. rammel

    iftekhar Guest

    You can try this, (did not compile )
    class1.h
    -----------
    #ifndef CLASS1_H_
    #define CLASS1_H_

    class class2; // and forward
    class class1
    {
    ....
    }
    #include "class2.h" // inclusion

    same for the "class2.h"

    it should solve your problems

    cheers
    iftekhar


    rammel wrote:
    > class1.h
    > -----------
    > #ifndef CLASS1_H_
    > #define CLASS1_H_
    > #include "class2.h" // inclusion
    > class class2; // and forward
    >
    > class class1
    > {
    > private:
    > class2 aRererence;
    > // --- gcc says: "error: field 'aRererence' has incomplete type" ---
    > public:
    > class1() :aReference( class2& aR ) {aReference = aR;};
    > // --- error according to the missing type of aReference ---
    > };
    > #endif /*CLASS1_H_*/
    >
    > ------------
    > class2.h
    > ------------
    > #ifndef CLASS2_H_
    > #define CLASS2_H_
    > #include <vector>
    > #include "class1.h" // inclusion
    > class class1; // and forward
    > class class2
    > {
    > private:
    > std::vector<class1> someContainer;
    > public:
    > class2();
    > };
    > #endif /*CLASS2_H_*/
    > -----------------------------------------
    >
    > What is the correct way around this error? I guess I could declare the
    > field as pointer, but I wanted to use the c++ reference mechanism.
    > Thanks for your help in advance.
     
    iftekhar, Nov 24, 2006
    #9
  10. rammel

    peter koch Guest

    rammel skrev:
    > class1.h
    > -----------
    > #ifndef CLASS1_H_
    > #define CLASS1_H_
    > #include "class2.h" // inclusion
    > class class2; // and forward


    There can never be a reason to both forwarddeclare and include the
    declaration.
    >
    > class class1
    > {
    > private:
    > class2 aRererence;

    In this case when declaring the member by value, you do need the
    complete type so gcc is right.

    > // --- gcc says: "error: field 'aRererence' has incomplete type" ---


    Because it does not see the complete definition of class2.
    > public:
    > class1() :aReference( class2& aR ) {aReference = aR;};
    > // --- error according to the missing type of aReference ---
    > };
    > #endif /*CLASS1_H_*/
    >
    > ------------
    > class2.h
    > ------------
    > #ifndef CLASS2_H_
    > #define CLASS2_H_
    > #include <vector>
    > #include "class1.h" // inclusion
    > class class1; // and forward
    > class class2
    > {
    > private:
    > std::vector<class1> someContainer;


    Here the standard also requires a complete definition. So you can't
    portably do what you want. The reason is that class2 (potentially)
    contains a class1 that contains a class2 that contains a class1
    that.... I believe you can se what these recursive definitions leads
    to.
    To solve it, yoy could e.g. look up pimpl.

    /Peter
    [snip]
     
    peter koch, Nov 24, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Suzanne Vogel
    Replies:
    2
    Views:
    2,619
    Suzanne Vogel
    Jun 26, 2003
  2. crichmon
    Replies:
    3
    Views:
    764
    crichmon
    Jun 28, 2004
  3. Dylan
    Replies:
    7
    Views:
    566
    Dylan
    Jul 7, 2004
  4. Markus Dehmann
    Replies:
    6
    Views:
    889
    Andre Dajd
    Nov 1, 2004
  5. Kiuhnm
    Replies:
    16
    Views:
    745
    Jonathan Mcdougall
    Jan 3, 2005
Loading...

Share This Page