Cyclic Dependency problem

Discussion in 'C++' started by Dennis Lerche, May 11, 2004.

  1. Hi

    I have a problem regarding cyclic dependency, yeahh I know bad
    design. But right at this moment I can't see how it should be
    redesigned to avoid this. The problem is that I just can't get it to
    compile .....
    I have two classes each having their own header files, including each
    other. A forward decleration doesn't seem to be enough because they
    also call function calls within the classes. How do I solve this ???

    Example:

    foo.h
    -----
    #include foo1.h

    class foo{
    public:
    funcCalc(foo1 *temp);
    anotherCall();
    }

    foo.cpp
    -------

    foo::funcCalc(foo1 *temp){
    foo1->calcSomething(); //function call to the object foo1
    }

    foo::anotherCall(){}



    foo1.h
    -----
    #include foo.h

    class foo1{
    public:
    calcSomething();
    anotherFunction();

    }

    foo1.cpp
    -------

    foo1::calcSomething(foo *temp){
    }

    foo1::anotherFunction(){
    foo->anotherCall(); //function call to the object foo1
    }


    This is just a very simple example of the mess I'm dealing with. How
    do I solve this ??

    --
    Dennis
     
    Dennis Lerche, May 11, 2004
    #1
    1. Advertising

  2. "Dennis Lerche" <> wrote in message
    news:...
    >
    > Hi
    >
    > I have a problem regarding cyclic dependency, yeahh I know bad
    > design. But right at this moment I can't see how it should be
    > redesigned to avoid this. The problem is that I just can't get it to
    > compile .....
    > I have two classes each having their own header files, including each
    > other. A forward decleration doesn't seem to be enough because they
    > also call function calls within the classes. How do I solve this ???
    >
    > Example:
    >
    > foo.h
    > -----
    > #include foo1.h
    >
    > class foo{
    > public:
    > funcCalc(foo1 *temp);
    > anotherCall();
    > }
    >
    > foo.cpp
    > -------
    >
    > foo::funcCalc(foo1 *temp){
    > foo1->calcSomething(); //function call to the object foo1
    > }
    >
    > foo::anotherCall(){}
    >
    >
    >
    > foo1.h
    > -----
    > #include foo.h
    >
    > class foo1{
    > public:
    > calcSomething();
    > anotherFunction();
    >
    > }
    >
    > foo1.cpp
    > -------
    >
    > foo1::calcSomething(foo *temp){
    > }
    >
    > foo1::anotherFunction(){
    > foo->anotherCall(); //function call to the object foo1
    > }
    >
    >
    > This is just a very simple example of the mess I'm dealing with. How
    > do I solve this ??
    >
    > --
    > Dennis


    Merge the two header files (when you have cyclic dependencies you should
    usually do this, and I don't think a cyclic dependencies automatically means
    bad design).

    Like this

    // combined foo header file
    class foo1;

    class foo{
    public:
    funcCalc(foo1 *temp);
    anotherCall();
    };

    class foo1{
    public:
    calcSomething();
    anotherFunction();
    };

    Any inline function calls can go after foo and foo1 within the header file.

    john
     
    John Harrison, May 11, 2004
    #2
    1. Advertising

  3. "John Harrison" <> writes:


    > Any inline function calls can go after foo and foo1 within the header file.
    >
    > john


    Thanks

    That works :eek:)

    Dennis
     
    Dennis Lerche, May 11, 2004
    #3
  4. Uzytkownik "Dennis Lerche" <> napisal w wiadomosci
    news:...
    >
    > Hi
    >
    > I have a problem regarding cyclic dependency, yeahh I know bad
    > design. But right at this moment I can't see how it should be
    > redesigned to avoid this. The problem is that I just can't get it to
    > compile .....
    > I have two classes each having their own header files, including each
    > other. A forward decleration doesn't seem to be enough because they
    > also call function calls within the classes. How do I solve this ???
    >
    > Example:
    >
    > foo.h
    > -----
    > #include foo1.h
    >
    > class foo{
    > public:
    > funcCalc(foo1 *temp);
    > anotherCall();
    > }
    >
    > foo.cpp
    > -------
    >
    > foo::funcCalc(foo1 *temp){
    > foo1->calcSomething(); //function call to the object foo1
    > }
    >
    > foo::anotherCall(){}
    >
    >
    >
    > foo1.h
    > -----
    > #include foo.h
    >
    > class foo1{
    > public:
    > calcSomething();
    > anotherFunction();
    >
    > }
    >
    > foo1.cpp
    > -------
    >
    > foo1::calcSomething(foo *temp){
    > }
    >
    > foo1::anotherFunction(){
    > foo->anotherCall(); //function call to the object foo1
    > }
    >
    >
    > This is just a very simple example of the mess I'm dealing with. How
    > do I solve this ??
    >
    > --
    > Dennis


    From your files I do not see why foo.h should include foo1.h?

    Foo.h only declares a member function that takes pointer to foo1. For this
    to compile you only need a forward delaration of foo1. Because you say that
    fwd declaration is not sufficient, you probably call foo1 member functions
    in foo.h, but this is not in the sample files that you supplied.

    To resolve that perhaps it's enough to move definitions of all functions
    that use foo1 from foo.h into foo.cpp. There you can safely include anything
    you want. What you lose is inlining of such functions, unless your compiler
    supports cross-module inlining.

    regards,
    Marcin
     
    Marcin Kalicinski, May 11, 2004
    #4
  5. Dennis Lerche

    nong-van-nghia

    Joined:
    Apr 6, 2010
    Messages:
    2
    Location:
    Vietnam
    Hi Dennis Lerche,

    I would like to contribute one more solution for your problem.
    I do this just for who got the same this problem, but need a better solution which does not break the current layout like: "Merge the two header files.." of John Harrison, "...move definitions of all functions that use foo1 from foo.h into foo.cpp" of Marcin Kalicinski

    Here is my solution:

    I) BRIEF
    =============================================================
    1) Replace "#include foo1.h" by declaring class foo1;
    2) At file foo.cpp, include foo1.h as the defining class foo1

    II) DETAIL
    =============================================================
    foo.h
    ------------------------------------------------------------
    class foo1; /* REPLACES #include foo1.h */

    class foo{
    public:
    funcCalc(foo1 *temp);
    anotherCall();
    }

    foo.cpp
    ------------------------------------------------------------
    ...
    #include foo1.h /* AS THE DEFINING OF class foo1 */

    foo::funcCalc(foo1 *temp){
    foo1->calcSomething(); //function call to the object foo1
    }

    foo::anotherCall(){}



    foo1.h
    ------------------------------------------------------------
    #include foo.h

    class foo1{
    public:
    calcSomething();
    anotherFunction();

    }

    foo1.cpp
    ------------------------------------------------------------

    foo1::calcSomething(foo *temp){
    }

    foo1::anotherFunction(){
    foo->anotherCall(); //function call to the object foo1
    }
    =============================================================

    Feel free to contact me by emailing to nghia4e at gmail dot com.

    Cheers,
    Nghia Nong (Mr.) from duongdua.vn
     
    nong-van-nghia, Apr 6, 2010
    #5
  6. Dennis Lerche

    nong-van-nghia

    Joined:
    Apr 6, 2010
    Messages:
    2
    Location:
    Vietnam
    Cyclic Dependency problem - include header file at cpp file

    Hi Dennis Lerche,

    I would like to contribute one more solution for your problem.
    I do this just for who got the same this problem, but need a other solution which does not break the current layout like: "Merge the two header files.." of John Harrison, "...move definitions of all functions that use foo1 from foo.h into foo.cpp" of Marcin Kalicinski

    Here is my solution:

    I) BRIEF
    =============================================================
    1) Replace "#include foo1.h" by declaring class foo1;
    2) At file foo.cpp, include foo1.h as the defining class foo1

    II) DETAIL
    =============================================================
    foo.h
    ------------------------------------------------------------
    class foo1; /* REPLACES #include foo1.h */

    class foo{
    public:
    funcCalc(foo1 *temp);
    anotherCall();
    }

    foo.cpp
    ------------------------------------------------------------
    ...
    #include foo1.h /* AS THE DEFINING OF class foo1 */

    foo::funcCalc(foo1 *temp){
    foo1->calcSomething(); //function call to the object foo1
    }

    foo::anotherCall(){}



    foo1.h
    ------------------------------------------------------------
    #include foo.h

    class foo1{
    public:
    calcSomething();
    anotherFunction();

    }

    foo1.cpp
    ------------------------------------------------------------

    foo1::calcSomething(foo *temp){
    }

    foo1::anotherFunction(){
    foo->anotherCall(); //function call to the object foo1
    }
    =============================================================

    Feel free to contact me by emailing to nghia4e at gmail dot com.

    Cheers,
    Nghia Nong (Mr.) from duongdua.vn
     
    nong-van-nghia, Apr 6, 2010
    #6
  7. Dennis Lerche

    Khaled.Jouda

    Joined:
    Jul 4, 2010
    Messages:
    1
    Solution

    Code:
    //foo,h
    #ifndef FOO_H
    #define FOO_H
    
    #include <iostream>
    
    #include "foo2.h"
    
    class Foo2;
    
    class Foo{
        public:
    	Foo2* foo2;
    	Foo();
            const char* say();
    };
    
    #endif
    
    the important thing is to write the implementation outside of the *.h files...

    Code:
    //foo.cpp
    #include "foo.h"
    
    Foo::Foo(){
    	std::cout << "Foo constructor" << std::endl;
    	this->foo2 = new Foo2();
    	this->foo2->say();
    }
    
    const char* Foo::say(){
        return "Hello From Foo";
    }
    
    Code:
    //foo2.h
    #ifndef FOO2_H
    #define FOO2_H
    
    #include "foo.h"
    
    class Foo;
    
    class Foo2{
    	public:
    
    	Foo2();
    	void shout(Foo foo);
    	void say();
    };
    
    #endif
    
    Code:
    //foo2.cpp
    #include <iostream>
    #include "foo2.h"
    Foo2::Foo2(){
    	std::cout << "Foo2 Constructor"<< std::endl;
    }
    void Foo2::shout(Foo foo){
    	std::cout << foo.say() << std::endl;
    }
    
    void Foo2::say(){
    	std::cout << "Foo2 said .." << std::endl;
    }
    
    Code:
    //main.cpp
    #include <iostream>
    
    #include "foo.h"
    #include "foo2.h"
    
    
    int main(int argc, char** args){
    	Foo f;
    	f.foo2->shout(f);
    	return 0;
    }
    
    Code:
    g++ main.cpp foo2.cpp foo.cpp  -o cyclic
    
    Output
    Code:
    Foo constructor
    Foo2 Constructor
    Foo2 said ..
    Hello From Foo
    
     
    Khaled.Jouda, Jul 5, 2010
    #7
    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. Nick Forrington

    Cyclic dependency

    Nick Forrington, Nov 16, 2004, in forum: C++
    Replies:
    6
    Views:
    485
    Nick Forrington
    Nov 17, 2004
  2. Replies:
    3
    Views:
    644
    Michael Mair
    Apr 22, 2006
  3. Replies:
    4
    Views:
    444
    Sakis
    May 17, 2006
  4. pallav

    Cyclic header dependency

    pallav, Apr 12, 2007, in forum: C++
    Replies:
    1
    Views:
    530
  5. pallav

    Cyclic header dependency

    pallav, Apr 12, 2007, in forum: C++
    Replies:
    3
    Views:
    403
    Michael
    Apr 13, 2007
Loading...

Share This Page