Results 1 to 14 of 14

Thread: circular definitions

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    circular definitions

    its late and i don't feel like thinking about how i need to go about doing this so anyone who has run into this problem before could you please help?

    i have two classes that refer back to each other and it can't compile in the normal fashion

    thanx
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  2. #2
    amac
    Guest
    What is the error?

    Are you using pointers to the other class, because you might be able to use forward declarations...

  3. #3
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    class b;
    class a {
    b* pb; };
    class b {
    a* pa; };
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    yes, thank you, that works

    the only problem is im using a member function from one of the classes and its telling me the function doesn't exist?

    would i need to do something like this?

    class b
    {
    void nothing();
    };

    class a
    {
    void func()
    {b->nothing();}

    b* pb;
    };

    class b
    {
    void nothing()
    { cout << "nothing";}

    a* pa;
    };
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  5. #5
    amac
    Guest
    Originally posted by CornedBee
    class b; <- This is a forward declaration.
    class a {
    b* pb; };
    class b {
    a* pa; };
    That was just for DNA's info...

    Umm the only thing I can suggest is... Do not "inline" the function that way. Move its definition into a source file. If you still want it inline you can specify that by placing the keyword "inline" before the function header.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    ok, well the function is in a seperate file, i just did that for my little example
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    ok i think i got it now

    i need to include all the .cpp code after ALL of the headers have been defined
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837
    i was wrong, it still doesn't work

    i wish there were more people on this forum
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    always include all headers first.
    inline functions are static by definition, so
    Code:
    // a .h file, included by both .cpp files
    class a{
    public:
      void foo();
    };
    // one .cpp file:
    inline void a::foo()
    {
      // code
    }
    // another .cpp file:
    void main()
    {
      a obj;
      obj.foo();  // linker error!
    }
    The functions only exist in the module they were defined in. Solution: put the functions in a special file (usual extension is .inl), then include this file at the end of the header.
    Code:
    // a.h
    class a{
    public:
     void foo();
    };
    #include "a.inl"
    
    // a.inl
    inline void foo()
    {
      // foo
    }
    
    // some.cpp
    void main()
    {
      a obj;
      obj.foo();  // OK
    }
    
    // someother.cpp
    void func()
    {
      a obj;
      obj.foo();  // OK
    }
    You could also place the code in a.inl directly in a.h, but the separation makes it more readable.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    Addicted Member
    Join Date
    Sep 2004
    Location
    Cleveland
    Posts
    148

    Re: circular definitions

    How about:

    class a
    {
    b thisb; // not a pointer!
    }

    class b
    {
    a* pa;
    }

    ???
    ParadoxResolved

    Go ahead, computer! Give me another Access Violation. . . I dare you!
    Here to chafe

  11. #11
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: circular definitions

    Possible, but you must reorder:
    Code:
    class a;
    class b {
     a *pa;
    };
    class a {
     b ob;
    };
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  12. #12
    Addicted Member
    Join Date
    Sep 2004
    Location
    Cleveland
    Posts
    148

    Re: circular definitions

    Not sure why, but that doesn't seem to compile. . .

    //{{AFX_INCLUDES()
    #include "MyStatic.h"
    //}}AFX_INCLUDES

    class CInterfaceView : public CFormView
    {
    ...
    //{{AFX_DATA(CInterfaceView)
    ...
    CMyStatic m_Att1Val;
    ...
    //}}AFX_DATA
    ...
    }


    #include "interfaceView.h"
    class CInterfaceView;
    class CMyStatic: public CStatic
    {
    ...
    CInterfaceView* parentform;
    ...
    }


    That leads to over a thousand errors.
    ParadoxResolved

    Go ahead, computer! Give me another Access Violation. . . I dare you!
    Here to chafe

  13. #13
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: circular definitions

    Remove the #include "interfaceView.h" from MyStatic.h.

    There's also no reason to store the pointer you're storing. Just use GetParent (or GetParentWindow) and cast the result.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  14. #14
    Addicted Member
    Join Date
    Sep 2004
    Location
    Cleveland
    Posts
    148

    Re: circular definitions

    Sweet! Thanks again.
    ParadoxResolved

    Go ahead, computer! Give me another Access Violation. . . I dare you!
    Here to chafe

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width