473,471 Members | 1,868 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

forward declaration of classes

Hi,

I have a question about forward declaration of classes.
First of all, a simple working example, which compiles and works with no
problems.

///file class_a.h:/
#ifndef __CLASS_A_H__
#define __CLASS_A_H__

class classA
{
public:
int getX() const
{
return m_nX;
}
void setX(int nX)
{
m_nX = nX;
}
private:
int m_nX;
};

#endif

///file class_b.h:/
#ifndef __CLASS_B_H__
#define __CLASS_B_H__

*class classA; //forward declaration of classA class*

class classB
{
public:
classB(classA *pA): m_pA(pA){}
int getData() const;
void setData(int nData);
private:
classA *m_pA;
};

#endif

///file class_b.cpp:/
#include "class_b.h"
*#include "class_a.h" //here we need to include "class_a.h"*

int classB::getData() const
{
if(m_pA)
return m_pA->getX();

return -1; //let's assume, -1 is an error code
}

void classB::setData(int nData)
{
if(m_pA)
m_pA->setX(nData);
}

It is enough to just put "class classA;" in class_b.h file, since classB
does not have classA members (only pointer-to-classA member), so forward
declaration is enough for compiler, and "#include "class_a.h" is needed
only in implementation file.
That's fine, BUT...

Often you can see a class or a struct declared as:

///file class_a.h:/

#ifndef __CLASS_A_H__
#define __CLASS_A_H__

typedef class
{
public:
int getX() const
{
return m_nX;
}
void setX(int nX)
{
m_nX = nX;
}
private:
int m_nX;
} TClassA;

#endif

In this case the previous approach (all other files are the same) does
not work...
MS VC++ 7.1 gives me the following output:
class_a.h(17) : error C2371: 'classA' : redefinition; different basic types
class_b.h(1) : see declaration of 'classA'
class_b.cpp(7) : error C2027: use of undefined type 'classA'
class_b.h(1) : see declaration of 'classA'
class_b.cpp(7) : error C2227: left of '->getX' must point to
class/struct/union
class_b.cpp(15) : error C2027: use of undefined type 'classA'
class_b.h(1) : see declaration of 'classA'
class_b.cpp(15) : error C2227: left of '->setX' must point to
class/struct/union

So, my question is: Is there any way to use forward declaration for
classA to avoid using '#include "class_a.h"' in class_b.h file in the
second case?

Thank you!
Nov 23 '05 #1
1 5689
First of all, use _plain text_ to post here.

HappyHippy wrote:
I have a question about forward declaration of classes.
First of all, a simple working example, which compiles and works with no
problems.
You are lucky. You used reserved identifiers in your code and got away
with it (so far, at least according to you). You should be careful, you
know.
///file class_a.h:/
#ifndef __CLASS_A_H__
#define __CLASS_A_H__
The identifier '__CLASS_A_H__' is reserved. Just drop the damn leading
and trailing underscores to make it legitimate.
class classA
{
public:
int getX() const
{
return m_nX;
}
void setX(int nX)
{
m_nX = nX;
}
private:
int m_nX;
};

#endif

///file class_b.h:/
#ifndef __CLASS_B_H__
#define __CLASS_B_H__
Again, reserved identifiers.
*class classA; //forward declaration of classA class*
My guess is the asterisk is what Netscape inserts to indicate "bold".
Yet another reason to post _plain text_.

class classB
{
public:
classB(classA *pA): m_pA(pA){}
int getData() const;
void setData(int nData);
private:
classA *m_pA;
};

#endif

///file class_b.cpp:/
#include "class_b.h"
*#include "class_a.h" //here we need to include "class_a.h"*

int classB::getData() const
{
if(m_pA)
return m_pA->getX();

return -1; //let's assume, -1 is an error code
}

void classB::setData(int nData)
{
if(m_pA)
m_pA->setX(nData);
}

It is enough to just put "class classA;" in class_b.h file, since classB
does not have classA members (only pointer-to-classA member), so forward
declaration is enough for compiler, and "#include "class_a.h" is needed
only in implementation file.
Correct.
That's fine, BUT...

Often you can see a class or a struct declared as:

///file class_a.h:/

#ifndef __CLASS_A_H__
#define __CLASS_A_H__

typedef class
{
public:
int getX() const
{
return m_nX;
}
void setX(int nX)
{
m_nX = nX;
}
private:
int m_nX;
} TClassA;

#endif

In this case the previous approach (all other files are the same) does
not work...
Correct.
MS VC++ 7.1 gives me the following output:
class_a.h(17) : error C2371: 'classA' : redefinition; different basic types
class_b.h(1) : see declaration of 'classA'
class_b.cpp(7) : error C2027: use of undefined type 'classA'
class_b.h(1) : see declaration of 'classA'
class_b.cpp(7) : error C2227: left of '->getX' must point to
class/struct/union
class_b.cpp(15) : error C2027: use of undefined type 'classA'
class_b.h(1) : see declaration of 'classA'
class_b.cpp(15) : error C2227: left of '->setX' must point to
class/struct/union

So, my question is: Is there any way to use forward declaration for
classA to avoid using '#include "class_a.h"' in class_b.h file in the
second case?


No.

Victor
Nov 23 '05 #2

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

Similar topics

6
by: Steven T. Hatton | last post by:
Should I be able to forward declare something from a namespace different from the current one? For example the following code compiles: //testdriver.hpp #ifndef TESTDRIVER_HPP #define...
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:...
2
by: Plok Plokowitsch | last post by:
After over a decade of programming in C++ I seem to have missed some substantial point (mental note: am I getting too old for this?). A little bit of help would be *very* appreciated. I'm trying...
3
by: Michael Sgier | last post by:
Hello with the original code below I get the error: "forward declaration of `struct CPlayer'" class CPlayer; // what does this do? Instantiate the class CPlayer? // what's a forward...
11
by: Milind | last post by:
Hi, I was trying to implement a composition relation, somthing of the following type: class A { public: class B {
8
by: Heiner | last post by:
Hi! The following snippet class B; class A { public:
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: toton | last post by:
Hi, I have two namespace contains class InkFrame and PrefDialog respectively. PrefDialog needs InkFrame to show the dialog over the frame. It also stores a pointer to InkFrame inside it. Now I...
6
by: Hunk | last post by:
Hi I have a question on usage of forward declarations of templates. While searching through this group , people suggested using forward declarations and typedefs for templates as // in...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.