Cyclic Dependency problem

D

Dennis Lerche

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 ??
 
J

John Harrison

Dennis Lerche said:
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 ??

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
 
M

Marcin Kalicinski

Uzytkownik "Dennis Lerche said:
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 ??

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
 
Joined
Apr 6, 2010
Messages
2
Reaction score
0
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
 
Joined
Apr 6, 2010
Messages
2
Reaction score
0
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
 
Joined
Jul 4, 2010
Messages
1
Reaction score
0
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top