H
HappyHippy
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!
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!