Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C++ MFC VC++ CLI
Hi All
I am facing a problem.
let suppose i have two classes.class A and class B.
in class A i defines the object of class B,and in class B i defines the object for class A.
When i try to run the code it show the error that to place a semicolon before the classA .
the code is something like this .

#include"B_wnd.h"
class A_wnd{
int xy;
B_wnd b_obj;
};







in class B declaration i am using like this



#include"A_wnd.h"
class B_wnd{
int bx;
A_wnd a_obj;
};




when i try to ru the code ,i get the error.
can any one help me in getting rid off from this problem.
Posted 25-Dec-11 5:49am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

That's a circular reference, and you can't do that. Refactor your code.
  Permalink  
Comments
vaibhavbvp at 25-Dec-11 10:52am
   
Hi Sir
can you help me to refactor this code,beacuse i ahve no idea how to overcome this
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

#include"B_wnd.h"
class A_wnd{
int xy;
B_wnd b_obj;
};
 
<h1>include"A_wnd.h"</h1>
 
class B_wnd{
int bx;
A_wnd a_obj;
};

First of all why do you need a circular reference? Any real need or you want just to test?

First of all the headers will have circular reference. You can use a include guard to over come that. Mostly all advanced C/C++ processors can avoid it, but always when you include headers use include guards. In Microsoft VCC then I think you can use
#pragma once

After you overcome this you have a second problem.

Suppose you create an object of type A_wnd.
When the constructor of A_wndis called it calls the constructors of B_wnd. Inside this again constructor of A_wnd will be called. This is a recursive problem. You will go out of stack and there will be crash.

You can use pointers or reference instead of real objects.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Use this:
//A_wnd.h
<h1>ifndef A_H</h1>
 
<h1>define A_H</h1>
 
<h1>include "B_wnd.h"</h1>
 
class B_wnd;
 
class A_wnd
{
    public:
    B_wnd* m_b;
};
 
<h1>endif</h1>
 
 
//B_wnd.h
<h1>ifndef B_H</h1>
 
<h1>define B_H</h1>
 
<h1>include "A_wnd.h"</h1>
 
class A_wnd;
 
class B_wnd
{
    public:
    A_wnd* f;
};
<h1>endif</h1>
 

2 important issues:
1. ifndef - to avoid redefinition
2. Forward definition (class B_wnd; for example).
  Permalink  
v2
Comments
Shakti Misra at 25-Dec-11 12:41pm
   
Nice, but you need not use the #include "A_wnd.h" and #include "B_wnd.h" if you want to use pointers. This will reduce some work for the pre processor engine.
arielb at 26-Dec-11 13:56pm
   
thanks, but if u will not include not A nor B, how will the classes know each other exist?
i tried removing the includes - didnt work.
Espen Harlinn at 31-Dec-11 8:26am
   
5'ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 310
3 ProgramFOX 265
4 CHill60 200
0 OriginalGriff 490
1 Sergey Alexandrovich Kryukov 405
2 Maciej Los 285
3 ProgramFOX 265
4 Peter Leow 210


Advertise | Privacy | Mobile
Web01 | 2.8.150331.1 | Last Updated 25 Dec 2011
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100