473,394 Members | 1,916 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,394 software developers and data experts.

loop in class definition ?

Hello
I have:

classA.h file:
#include "classB.h"
classA{
classB *ptr;
};

classA.c file:
#include "classA.h"
.......

classB.h file:
#include "classA.h"
classB{
classA *ptr;
}

classB.c file:
#include "classB.h"
.......

I compile classA.c and classB.c and later link all together.
My problem is that in this case there is redefinition of classA and
classB (because classA.h and classB.h files are included two times).
When i used in *.h files:
#ifndef XXX
#define XXX
......
#endif

*.h files are included once but i do not see their definitions and can
not compile (classB.h do not see definition for classA and classA.h do
not see definition for classB).

How can i solve this problem ?

Thanx
Michal
Jul 22 '05 #1
1 2534
On Sat, 28 Aug 2004 14:23:47 +0200, vertigo wrote:
Hello
I have:

classA.h file:
#include "classB.h"
classA{
classB *ptr;
};

classA.c file:
#include "classA.h"
......

classB.h file:
#include "classA.h"
classB{
classA *ptr;
}

classB.c file:
#include "classB.h"
......

I compile classA.c and classB.c and later link all together.
My problem is that in this case there is redefinition of classA and
classB (because classA.h and classB.h files are included two times).
When i used in *.h files:
#ifndef XXX
#define XXX
.....
#endif

*.h files are included once but i do not see their definitions and can
not compile (classB.h do not see definition for classA and classA.h do
not see definition for classB).

How can i solve this problem ?


Use forward declarations:

classA.h:
class B;
class A
{
class B* ptr;
};

classA.c:
#include "classA.h"
#include "classB.h"
....

(and do the same for classB obviously).

This is covered by the C++ FAQ Lite:

http://www.parashift.com/c++-faq-lit...html#faq-38.11

Jul 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Gaurav Veda | last post by:
Hi ! I am a poor mortal who has become terrified of Python. It seems to have thrown all the OO concepts out of the window. Penniless, I ask a basic question : What is the difference between a...
12
by: Bryan Parkoff | last post by:
CMain Class is the base class that is initialized in main function. CA Class is the base class that is initialized in CMain::CMain(). CMain Class is always public while CA Class is always...
15
by: Robin Eidissen | last post by:
What I try to do is to iterate over two variables using template metaprogramming. I've specialized it such that when it reaches the end of a row ot starts on the next and when it reaches the last...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
7
by: Curious | last post by:
Hi, I have a C# class that contains a property defined as follows: public bool bRunTwice { get { return bRunTwice; } set { bRunTwice = value; } }
0
by: rich | last post by:
Hi all, I have a fairly complex "feed" application that recieves messages from an external user-supplied API via a callback function, and attempts to forward these messages to another...
9
by: Jess | last post by:
Hello, I was told that if I declare a static class constant like this: class A{ static const int x = 10; }; then the above statement is a declaration rather than a definition. As I've...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
6
by: Nobody | last post by:
hi there, suppose I want to iterate for a specific variable e.g. i , but for non regular (or consecutive) values. For example i=0,1,2,4,5,7,8 etc how can I do that with a for loop? MY...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.