473,396 Members | 1,900 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Circular inclusion problem (forward declare)

Hi,

I got the following circular inclusion problem, and hope that
someone could show me some help here.

Code:
==================
Expand|Select|Wrap|Line Numbers
  1.      1  #include <iostream>
  2.      2  #include <string>
  3.      3  using namespace std;
  4.      4
  5.      5  // class Welcome;
  6.      6  class Mediator;
  7.      7
  8.      8  class Welcome
  9.      9  {
  10.     10     public:
  11.     11          Welcome(Mediator* m)
  12.     12          {  mediator = m;  }
  13.     13
  14.     14          void go()
  15.     15          {  mediator->handle("Welcome !!");  }
  16.     16
  17.     17     private:
  18.     18          Mediator*       mediator;
  19.     19
  20.     20
  21.     21  };
  22.     22  class Mediator
  23.     23  {
  24.     24     public:
  25.     25          Mediator()
  26.     26          {  welcome = new Welcome(this);  }
  27.     27
  28.     28          void handle(string state)
  29.     29          {  cout << state << endl;  }
  30.     30
  31.     31          Welcome* getWelcome()
  32.     32          {  return welcome; }
  33.     33
  34.     34     private:
  35.     35          Welcome*        welcome;
  36.     36
  37.     37  };
  38.     38
  39.     39  int main()
  40.     40  {
  41.     41     Mediator* mediator = new Mediator();
  42.     42
  43.     43     mediator->getWelcome()->go();
  44.     44
  45.     45     delete mediator;
  46.     46  }
  47.  
The compile errors below:
224 unix-machine[**]:/class/PATTERN-DESIGN/mediator% g++ webpage1.cc
webpage1.cc: In member function 'void Welcome::go()':
webpage1.cc:15: error: invalid use of undefined type 'struct Mediator'
webpage1.cc:6: error: forward declaration of 'struct Mediator'
******************************************

I understand the forward declaring will not allow me to call the member function on line 15, how do I go about resolving this on line 15? I tried to swap the placement of 2 classes, but no avail.

thanks,

Tera
Jan 9 '12 #1

✓ answered by Banfa

To resolve a circular dependence problem you are on the right track. However you need to be aware of what a forward declaration does. It informs the compiler that a structure or class with the given name exists and this allows the compiler to create pointers or references to that class. What it does not do is allow the compiler to access any members of the class or anything to do with the classes structure (for example its size through the sizeof operator).

So you have a problem in Welcome::go because you try to access the Mediator::handle member that the compiler knows nothing about.

Your mistake is to combine the declaration of the class and its methods and definition of those methods. While that works for normal classes for classes in a circular dependency it doesn't for the very reason that your code doesn't; in the method definitions you do not have all the declarations you need to compile them. You need to declare everything first and only then define the methods.

If you split you classes so that they are each in a header and a cpp file then you can use forward declaration to allow you to declare the 2 classes successfully and having done that you can then define their methods without trouble in the presence of the full definition of both classes.

So for example you Welcome class would become (you would need to do this for mediator as well)

Expand|Select|Wrap|Line Numbers
  1. // Welcome Header
  2. class Mediator;
  3.  
  4. class Welcome
  5. {
  6. public:
  7.   Welcome(Mediator* m);
  8.  
  9. private:
  10.   Mediator*       mediator;
  11. };
  12.  
Expand|Select|Wrap|Line Numbers
  1. #include "welcome.h"
  2. #include "mediator.h"
  3.  
  4. Welcome::Welcome(Mediator* m)
  5. {
  6.   mediator = m;
  7. }
  8.  
  9. void Welcome::go()
  10. {
  11.   mediator->handle("Welcome !!");
  12. }
  13.  
Note that in this code the Welcome header only uses a pointer to Mediator so a forward declaration works. In the code file by the time the members of Mediator are used in Welcome::go the Mediator class has been declared in its header so the compiler wont have a problem.

3 3246
Banfa
9,065 Expert Mod 8TB
To resolve a circular dependence problem you are on the right track. However you need to be aware of what a forward declaration does. It informs the compiler that a structure or class with the given name exists and this allows the compiler to create pointers or references to that class. What it does not do is allow the compiler to access any members of the class or anything to do with the classes structure (for example its size through the sizeof operator).

So you have a problem in Welcome::go because you try to access the Mediator::handle member that the compiler knows nothing about.

Your mistake is to combine the declaration of the class and its methods and definition of those methods. While that works for normal classes for classes in a circular dependency it doesn't for the very reason that your code doesn't; in the method definitions you do not have all the declarations you need to compile them. You need to declare everything first and only then define the methods.

If you split you classes so that they are each in a header and a cpp file then you can use forward declaration to allow you to declare the 2 classes successfully and having done that you can then define their methods without trouble in the presence of the full definition of both classes.

So for example you Welcome class would become (you would need to do this for mediator as well)

Expand|Select|Wrap|Line Numbers
  1. // Welcome Header
  2. class Mediator;
  3.  
  4. class Welcome
  5. {
  6. public:
  7.   Welcome(Mediator* m);
  8.  
  9. private:
  10.   Mediator*       mediator;
  11. };
  12.  
Expand|Select|Wrap|Line Numbers
  1. #include "welcome.h"
  2. #include "mediator.h"
  3.  
  4. Welcome::Welcome(Mediator* m)
  5. {
  6.   mediator = m;
  7. }
  8.  
  9. void Welcome::go()
  10. {
  11.   mediator->handle("Welcome !!");
  12. }
  13.  
Note that in this code the Welcome header only uses a pointer to Mediator so a forward declaration works. In the code file by the time the members of Mediator are used in Welcome::go the Mediator class has been declared in its header so the compiler wont have a problem.
Jan 9 '12 #2
Hi, Banfa:

Thanks for the response.

Wonder if there is a way to resolve this circular dependency problem with simply one file (ie, no header and implementation files separately), with all the methods being in-lined in header file?

Thx,

TR
Jan 9 '12 #3
Banfa
9,065 Expert Mod 8TB
You could do your file would have to have this layout

Expand|Select|Wrap|Line Numbers
  1. // Declaration of first class
  2.  
  3. // Declaration of second class
  4.  
  5. // Definition of methods of first and second class
  6.  
It would be quite messy in my opinion.
Jan 10 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: amit gulati | last post by:
Is it possible to forward declare a template class using "class XXX". If yes, then what is the syntax??
6
by: Markus Dehmann | last post by:
I have a circular dependency between two classes. The FAQ hint about a forward declaration does not help in my case ( How can I create two classes that both know about each other?) Error:...
9
by: Divick | last post by:
Hi all, does any one know what is the right way to forward declare classes within namespaces. Though I have been using the syntax as follows but it doesn't sound good to me. namespace...
4
by: yuliy | last post by:
Hello gurus, I stuck in following: how can I do forward declaration if the forward declared class is in some namespace? something like // header class std::string; // approach#1
23
by: mark.moore | last post by:
I know this has been asked before, but I just can't find the answer in the sea of hits... How do you forward declare a class that is *not* paramaterized, but is based on a template class? ...
1
by: yancheng.cheok | last post by:
currently, i have a private function in cat named privateFun. i would like to have this function "private" to all except dog's action member function. by using the following approach, all the...
9
by: rammel | last post by:
class1.h ----------- #ifndef CLASS1_H_ #define CLASS1_H_ #include "class2.h" // inclusion class class2; // and forward class class1 { private:
20
by: Binary | last post by:
Hi, With keyword struct, we can simply do forward declare by: struct struct_a; but if we typedef it: typdef struct struct_a struct_a_t;
6
by: matrem | last post by:
I wish to make a method of a base class a friend of a method of a derived class. However, this makes me quickly run into a circular dependency problem. I must implement the base class first, but it...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.