Say, I have two classes: A
&& B
; One Header file myheader.h
:
....
class A{
public:
void display(B* c);
};
class B{
public:
void display(A* c);
};
.....
Compiler gave me error: 'B' has not been declared( in A::display )As expected.
So I wrote two separated headers for A
and B
: aheader.h
(including definition of class A) and bheader.h
(including class B);
In aheader.h
:
#include "bheader.h"
In bheader.h
:
#include "aheader.h"
But the problem occurs when I start to write the implementation.cpp
:
#include "aheader.h"
#include "bheader.h"
void A::display(B* c){}
void B::display(A* c){}
Now,A' has not been declared.(in B::display)
Don't know how to describe this problem in brief.
I'm using Ubuntu14,Eclipse CDT,Linux GCC & Gnu Makd Builder.
I'm new to C++,I guess this problem occurs when linking. And I really hope someone to give me an explanation or a solution. Thank you !
Instead of mutually including the header files in each other, use forward declarations. In aheader.h
:
class B;
class A{
public:
void display(B* c);
};
And in bheader.h
:
class A;
class B{
public:
void display(A* c);
};
This is quite common when beginning with C / C++
The usual aproach in C++ is to have all the unrelated classes separated, each one in its own header file. Always (I mean it, ALWAYS) the header file MUST be protected against recursive inclusion. This is done by appending an IFDEF at the beginning of the header file, so the file is included only once:
I. e. file aheader.h:
#ifndef __AHEADER_H__
#define __AHEADER_H__
//All the header code comes here...
#endif
So when including the header file, the compiler will do the following:
__AHEADER_H__
defined?__AHEADER_H__
now.__AHEADER_H__
defined?Ok, this is one part of your problem. But lets go to your actual problem.
You are having the error because, as the compiler tells you, the other class is not defined. In other words, the compiler does not know what A (or B) is. You cannot include "aheader.h" in "bheader.h", since this will throw the "B is not defined" error:
Of course, if you include them the other way (bheader.h included in aheader.h) the error would be "A is not defined", just with the same explanation.
The solution?
Just forward define the class:
In bheader.h:
#ifndef __BHEADER_H__
#define __BHEADER_H__
class A; //Forward definition. We don't have any member definition, just we are telling
//the compiler "Trust me, this is a class"
class B{
public:
void display(A* c);
}
#endif
The same would have to be done in aheader.h.
And finally, in order to be able to use A or B in your implementation code, you should include the actual header file of the class.
file implementation.cpp:
#include "aheader.h"
#include "bheader.h"
void A::display(B* c){}
//And so on...
Forward declaration can be used whenever you don't need to have the complete class definition (i.e.):
You cannot have forward declaration when you need to have the complete class definition (i.e.):
By the way, the usual way of separating classes would be also to separate the implementation into one .cpp file per class, having at the end:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With