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! 1 5700
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 TESTDRIVER_HPP
#include <ostream>
namespace ns_testdriver{
using std::ostream;
|
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: "field `foo' has incomplete type" for the following code:
#include <iostream>
#include <vector>
|
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 to gather various different classes into a common namespace
using typedefs:
class QWidget {};
class MyListview {};
namespace gui
|
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 declaration?
class CEnemy : public CEntity
{
public:
|
by: Milind |
last post by:
Hi,
I was trying to implement a composition relation, somthing of the
following type:
class A
{
public:
class B
{
| |
by: Heiner |
last post by:
Hi!
The following snippet
class B;
class A
{
public:
|
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?
Here's what I thought should work, but apparently doesn't:
class Foo;
void f1(Foo* p)
|
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 want InkFrame to be forward declared in the PrefDialog header
file rather than to be included. I want to include it in the cpp file
instead. There is no harm including it in PrefDialog header, and it
works. But I want to save some compilation time....
|
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 myfile.h
template<typename T,typename R>
class some_class;
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |