Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Cyclic Dependency problem

Reply
Thread Tools

Cyclic Dependency problem

 
 
Dennis Lerche
Guest
Posts: n/a
 
      05-11-2004

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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      05-11-2004

"Dennis Lerche" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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


 
Reply With Quote
 
 
 
 
Dennis Lerche
Guest
Posts: n/a
 
      05-11-2004
"John Harrison" <(E-Mail Removed)> writes:


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


Thanks

That works )

Dennis
 
Reply With Quote
 
Marcin Kalicinski
Guest
Posts: n/a
 
      05-11-2004

Uzytkownik "Dennis Lerche" <(E-Mail Removed)> napisal w wiadomosci
news:(E-Mail Removed)...
>
> 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


 
Reply With Quote
 
nong-van-nghia nong-van-nghia is offline
Junior Member
Join Date: Apr 2010
Location: Vietnam
Posts: 2
 
      04-06-2010
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
 
Reply With Quote
 
nong-van-nghia nong-van-nghia is offline
Junior Member
Join Date: Apr 2010
Location: Vietnam
Posts: 2
 
      04-06-2010
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
 
Reply With Quote
 
Khaled.Jouda Khaled.Jouda is offline
Junior Member
Join Date: Jul 2010
Posts: 1
 
      07-04-2010
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Cyclic header dependency pallav C++ 1 04-13-2007 06:37 AM
Cyclic header dependency pallav C++ 3 04-13-2007 03:58 AM
Cyclic Creation Dependency ? sakis.panou@btinternet.com C++ 4 05-17-2006 01:12 PM
Questions regarding cyclic header dependency fc2004@gmail.com C Programming 3 04-22-2006 07:06 AM
Cyclic dependency Nick Forrington C++ 6 11-17-2004 12:18 AM



Advertisments