--EDIT --
So sorry that I confused people, I just quickly typed this code out instead of copy and pasting, so I actually do #ifndef A_H #define A_H in my code. Ive changed the below code to show that
-- End edit --
I have two classes which each contain a pointer to an instance of the other class, but this is creating problems for me . My code is similar to the following
// A.h
#ifndef A_H
#define A_H
class B; // compiler error here
class A
{
B* foo;
// other members and functions
};
#endif
// A.cpp
#include "A.h"
#include "B.h"
/*
declare functions and use methods in both A and B
*/
// B.h
#ifndef B_H
#define B_H
class A;
class B
{
A** bar;
// other stuff
};
#endif
//B.cpp
#include "A.h"
#include "B.h"
/*
declare functions and use methods in both A and B
*/
I was told that forward declaring the other class int he header file then including the other file in the cpp file would work, but on the marked line I get an error that just says "forward declaration of 'struct b'"
Can anyone tell me what I'm doing wrong?
Include one header, let say b.h in a.h. Do not forward declare B in a.h. b.h can stay as it is.
Otherwise you get sth like
class B {};
....
class B;
It is always wise to do preprocessing only on such errors.
Related
I tested the following code(just for test, I know it's a bad idea), and I couldn't understand why it has an error. Please help me!
There are two classes A and B, both in separate hpp files. I let them include each other(I think it doesn't matter because of the include guard), and it didn't go wrong until I added the definition of B::fun in B.cpp.
A.h
#ifndef A_H
#define A_H
#include "B.h"
class A {
friend void B::fun(A&);
};
#endif
B.h
#ifndef B_H
#define B_H
#include "A.h"
class A;
class B {
public:
void fun(A&);
};
#endif
B.cpp
#include "B.h"
void B::fun(A& a) {
}
main.cpp
#include "A.h"
#include "B.h"
int main() {
}
I run it with command like this:
g++ -g -o test main.cpp B.cpp
Error message
A.h:7:17: error: 'B' has not been declared
friend void B::fun(A&);
I know two workarounds for the symptoms, but I want to understand the problems root cause.
Method 1:
comment the code #include "A.h" in B.h
Method 2:
Modify #include "B.h" to #include "A.h" in B.cpp
I want to know why these workarounds avoid the symptoms, the exact reason for both methods.
Simply remove #include "A.hpp" from B.hpp. This is happening because when B.cpp includes B.hpp, the latter in turn includes A.hpp before B is defined. But you don't need to include A.hpp at all because you have correctly foward-declared class A.
You do not ask for a solution, you ask for the reasons that your two workarounds remove the error, good way of learning. My respect.
In order to understand why you get the error and why you can get rid of it with the two methods you found yourself, you should "play preprocessor" and do the including yourself, while observing the effect of your ifdefs, aka reinclusion guards.
Looking at how B.cpp gets compiled, insert the header files where you do the includes.
You get:
#ifndef B_H
#define B_H
#ifndef A_H
#define A_H
#include "B.h" /* not expanded, will be removed next step */
class A {
friend void B::fun(A&);
};
#endif
class A;
class B {
public:
void fun(A&);
};
#endif
void B::fun(A& a) {
}
Then remove those parts which are prevented by the #ifdefs, i.e. the second time of B header, you get:
#ifndef B_H
#define B_H
#ifndef A_H
#define A_H
class A {
friend void B::fun(A&); /* obviously unknown B */
};
#endif
class A;
class B {
public:
void fun(A&);
};
#endif
void B::fun(A& a) {
}
Now you see why you get the error in B.cpp.
Now why does method 1 work?
Lets do the same again, with that change applied:
#ifndef B_H
#define B_H
/* #include "A.h" */ /* method 1 */
class A;
class B {
public:
void fun(A&); /* fine, because forward declaration three lines above is sufficient */
};
#endif
void B::fun(A& a) {
}
Now, why does method 2 help?
/* method 2, include A.h before B.h into B.cpp */
#ifndef A_H
#define A_H
#ifndef B_H
#define B_H
/* #include "A.h" not expanded because of ifdef */
class A;
class B {
public:
void fun(A&); /* fine, because of forward declaration */
};
#endif
class A {
friend void B::fun(A&); /* fine of forward declaration B having been seen */
};
#endif
/* #include "B.h" not expanded, because of ifdef */
void B::fun(A& a) {
}
In short, both workarounds work by making the forward declaration be seen before using A and B being fully declared before being used.
To understand where the error comes from, just follow the "chain of includes" that happen when you compile B.cpp:
B.cpp includes B.h.
B.h includes A.h.
A.h tries to include B.h but doesn't, because you have correctly set up include guards.
Now the compiler comes to the line friend void B::fun(A&); in A.h, but at this stage B has not been declared!
This question already has answers here:
Resolve build errors due to circular dependency amongst classes
(12 answers)
Closed 7 years ago.
i have created two classes A and B so i have 5 files , main.cpp , A.h , A.cpp , B.h , B.cpp
i have included all headers as it should be and im trying to create a object of class A in class B and i get the following error : error: A doesn't name a type
and if i to it repeated like i define object B in class A it works ,,, whats wrong?
this is how my B.h looks like >
#ifndef B_H
#define B_H
#include <iostream>
#include "A.h"
using namespace std;
class B
{
public:
B();
protected:
private:
A instance;
};
#endif // B_H
and now A.h
#ifndef A_H
#define A_H
#include <iostream>
#include "B.h"
using namespace std;
class A
{
public:
A();
protected:
private:
};
#endif // A_H
Your B.h includes A.h and A.h includes B.h. This leads you to something like B.h includes B.h.
From A.h delete the include "B.h". It is unused.
Let's look at A.h:
First it includes B.h. This pulls in the definition of class B, which uses class A. Next it defines class A. There's the problem. As others have stated removing the unused include will fix the problem.
SOLVED
What really helped me was that I could #include headers in the .cpp file with out causing the redefined error.
I'm new to C++ but I have some programming experience in C# and Java so I could be missing something basic that's unique to C++.
The problem is that I don't really know what's wrong, I will paste some code to try to explain the issue.
I have three Classes, GameEvents, Physics and GameObject. I have headers for each of them.
GameEvents has one Physics and a list of GameObjects.
Physics has a list of GameObjects.
What I'm trying to achieve is that I want GameObject to be able to access or own a Physics object.
If I simply #include "Physics.h" in GameObject I get the
"error C2111: 'ClassXXX' : 'class' type redifinition" which I understand.
And this is where I thought #include-guards would help so I added an include guard to my Physics.h since that's the header I want to include twice.
This is how it looks
#ifndef PHYSICS_H
#define PHYSICS_H
#include "GameObject.h"
#include <list>
class Physics
{
private:
double gravity;
list<GameObject*> objects;
list<GameObject*>::iterator i;
public:
Physics(void);
void ApplyPhysics(GameObject*);
void UpdatePhysics(int);
bool RectangleIntersect(SDL_Rect, SDL_Rect);
Vector2X CheckCollisions(Vector2X, GameObject*);
};
#endif // PHYSICS_H
But if I #include "Physics.h" in my GameObject.h now like this:
#include "Texture2D.h"
#include "Vector2X.h"
#include <SDL.h>
#include "Physics.h"
class GameObject
{
private:
SDL_Rect collisionBox;
public:
Texture2D texture;
Vector2X position;
double gravityForce;
int weight;
bool isOnGround;
GameObject(void);
GameObject(Texture2D, Vector2X, int);
void UpdateObject(int);
void Draw(SDL_Surface*);
void SetPosition(Vector2X);
SDL_Rect GetCollisionBox();
};
I get multiple issues that don't understand why they're showing up.
If I don't #include "Physics.h" my code runs just fine.
I'm very grateful for any help.
The preprocessor is a program that takes your program, makes some changes (for example include files (#include), macro expansion (#define), and basically everything that starts with #) and gives the "clean" result to the compiler.
The preprocessor works like this when it sees #include:
When you write:
#include "some_file"
The contents of some_file almost literally get copy pasted into the file including it. Now if you have:
a.h:
class A { int a; };
And:
b.h:
#include "a.h"
class B { int b; };
And:
main.cpp:
#include "a.h"
#include "b.h"
You get:
main.cpp:
class A { int a; }; // From #include "a.h"
class A { int a; }; // From #include "b.h"
class B { int b; }; // From #include "b.h"
Now you can see how A is redefined.
When you write guards, they become like this:
a.h:
#ifndef A_H
#define A_H
class A { int a; };
#endif
b.h:
#ifndef B_H
#define B_H
#include "a.h"
class B { int b; };
#endif
So now let's look at how #includes in main would be expanded (this is exactly, like the previous case: copy-paste)
main.cpp:
// From #include "a.h"
#ifndef A_H
#define A_H
class A { int a; };
#endif
// From #include "b.h"
#ifndef B_H
#define B_H
#ifndef A_H // From
#define A_H // #include "a.h"
class A { int a; }; // inside
#endif // "b.h"
class B { int b; };
#endif
Now let's follow the preprocessor and see what "real" code comes out of this. I will go line by line:
// From #include "a.h"
Comment. Ignore! Continue:
#ifndef A_H
Is A_H defined? No! Then continue:
#define A_H
Ok now A_H is defined. Continue:
class A { int a; };
This is not something for preprocessor, so just leave it be. Continue:
#endif
The previous if finished here. Continue:
// From #include "b.h"
Comment. Ignore! Continue:
#ifndef B_H
Is B_H defined? No! Then continue:
#define B_H
Ok now B_H is defined. Continue:
#ifndef A_H // From
Is A_H defined? YES! Then ignore until corresponding #endif:
#define A_H // #include "a.h"
Ignore
class A { int a; }; // inside
Ignore
#endif // "b.h"
The previous if finished here. Continue:
class B { int b; };
This is not something for preprocessor, so just leave it be. Continue:
#endif
The previous if finished here.
That is, after the preprocessor is done with the file, this is what the compiler sees:
main.cpp
class A { int a; };
class B { int b; };
So as you can see, anything that can get #included in the same file twice, whether directly or indirectly needs to be guarded. Since .h files are always very likely to be included twice, it is good if you guard ALL your .h files.
P.S. Note that you also have circular #includes. Imagine the preprocessor copy-pasting the code of Physics.h into GameObject.h which sees there is an #include "GameObject.h" which means copy GameObject.h into itself. When you copy, you again get #include "Pysics.h" and you are stuck in a loop forever. Compilers prevent that, but that means your #includes are half-done.
Before saying how to fix this, you should know another thing.
If you have:
#include "b.h"
class A
{
B b;
};
Then the compiler needs to know everything about b, most importantly, what variables it has etc so that it would know how many bytes it should put in place of b in A.
However, if you have:
class A
{
B *b;
};
Then the compiler doesn't really need to know anything about B (since pointers, regardless of the type have the same size). The only thing it needs to know about B is that it exists!
So you do something called "forward declaration":
class B; // This line just says B exists
class A
{
B *b;
};
This is very similar to many other things you do in header files such as:
int function(int x); // This is forward declaration
class A
{
public:
void do_something(); // This is forward declaration
}
You have circular references here: Physics.h includes GameObject.h which includes Physics.h. Your class Physics uses GameObject* (pointer) type so you don't need to include GameObject.h in Physics.h but just use forward declaration - instead of
#include "GameObject.h"
put
class GameObject;
Furthermore, put guards in each header file.
The issue is that your GameObject.h does not have guards, so when you #include "GameObject.h" in Physics.h it gets included when GameObject.h includes Physics.h.
Add include guards in all your *.h or *.hh header files (unless you have specific reasons to not do that).
To understand what is happening, try to get the preprocessed form of your source code. With GCC, it is something like g++ -Wall -C -E yourcode.cc > yourcode.i (I have no idea on how Microsoft compilers do that). You can also ask which files are included, with GCC as g++ -Wall -H -c yourcode.cc
Firstly you need include guards on gameobject too, but that's not the real problem here
If something else includes physics.h first, physics.h includes gameobject.h, you get something like this:
class GameObject {
...
};
#include physics.h
class Physics {
...
};
and the #include physics.h gets discarded because of the include guards, and you end up with a declaration of GameObject before the declaration of Physics.
But that's a problem if you want GameObject to have a pointer to a Physics, because for htat physics would have to be declared first.
To resolve the cycle, you can forward-declare a class instead, but only if you are just using it as a pointer or a reference in the declaration following, i.e.:
#ifndef PHYSICS_H
#define PHYSICS_H
// no need for this now #include "GameObject.h"
#include <list>
class GameObject;
class Physics
{
private:
list<GameObject*> objects;
list<GameObject*>::iterator i;
public:
void ApplyPhysics(GameObject*);
Vector2X CheckCollisions(Vector2X, GameObject*);
};
#endif // PHYSICS_H
Use include guards in ALL your header files. Since you are using Visual Studio you could use the #pragma once as the first preprocessor definition in all your headers.
However I suggest to use the classical approach:
#ifndef CLASS_NAME_H_
#define CLASS_NAME_H_
// Header code here
#endif //CLASS_NAME_H_
Second read about forward declaration and apply it.
The goal of a header guard is to avoid including the same file many times.
But the header guard that is currently used in C ++ can be improved. The current guard is:
#ifndef AAA_H
#define AAA_H
class AAA
{ /* ... */ };
#endif
My new guard proposal is:
#ifndef AAA_H
#define AAA_H
class AAA
{ /* ... */ };
#else
class AAA; // Forward declaration
#endif
This resolves the annoying problem that occurs when the AAA class needs the BBB class declaration, while the BBB class needs the AAA class declaration, typically because there are crossed pointers from one class to the other:
// File AAA.h
#ifndef AAA_H
#define AAA_H
#include "BBB.h"
class AAA
{
BBB *bbb;
/* ... */
};
#else
class AAA; // Forward declaration
#endif
//+++++++++++++++++++++++++++++++++++++++
// File BBB.h
#ifndef BBB_H
#define BBB_H
#include "AAA.h"
class BBB
{
AAA *aaa;
/* ... */
};
#else
class BBB; // Forward declaration
#endif
I would love for this to be included in the IDEs that automatically generate code from templates.
" #pragma once " ::: serves the same purpose as header guards, and has the added benefit of being shorter and less error-prone.
Many compilers support a simpler, alternate form of header guards using the #pragma directive:
" #pragma once "
// your code here
However, #pragma once is not an official part of the C++ language, and not all compilers support it (although most modern compilers do).
For compatibility purposes, people recommend sticking to traditional header guards. They aren’t much more work and they’re guaranteed to be supported on all compliant compilers.
I'm working on a large code in c++ composed by a lot of .h and .c files.
The main problem is caused by a pair of class wich are supposed to link each other.
Due to declaration need in the software architecture, the first class (name it A) is initializated in an "upper level" class.
so we got something like:
#include A.h
class mainClass{
...
A a;
...
}
A.h looks like:
#ifndef A_H
#define A_H
#include B.h
class A{
A();
fooA();
...
private:
B b;
...
}
#endif
A.cpp looks like:
#include B.h
#include A.h
...
A::A(){
...
b(this) //here I get the first error that follows
...
}
A::fooA(){//do somthing}
In order to avoid mutual header inclusion in the second class (let it be B) I used forward declaration and a pointer var to class A.
B.h looks like:
#ifndef B_H
#define B_H
class A; //Forward declaration to A
class B{
B()
B(A* const t)
fooB();
A* a; //pointer to A object
}
B.cpp looks like:
#include B.h
B::B(){
//default constructor. Do Nothing
}
B::B(A* const t){
this->a=t //constructor that set the pointer to the object of type A
}
B::fooB(){
a->fooA(); //here i get the second error that follows
}
Now if in my Makefile I link A before B i get the compiling error:
//First error. See code above for line numbers
error: no match for call to ‘(B) (A* const)’
On the other hand if I link B before A i get the compiling error:
//Second error. see code above for line numbers
error: invalid use of incomplete type ‘struct A’
_B.h:'line of the forward declaration': error: forward declaration of ‘struct A’
I must admint i'm pretty new to c++ so I can't understand where I'm wrong.
EDIT
Now I'm using the solution:
use include guard
forward declare class A, and don't include A.h in B.h
include both B.h and A.h in A.cpp and B.cpp. Always include B.h before A.h
but I get the same error:
error: no match for call to ‘(B) (A* const)'
Could it be a constructor Overloading problem? If I remove the line
b(this)
The compiling works fine.
SOLVED
If a use an help function that set the variable A* a in B insted of using a constructor Everything works fine during compilation. Maybe I need to better understand constructor overloading in C++. Thank you very much.
use include guard
forward declare class A, and don't include A.h in B.h
include both B.h and A.h in A.cpp and B.cpp. Always include B.h before A.h
Try including "A.h" from B.cpp!
That will resolve your "A" type when you need to use A from B.cpp. Just make sure you don't do anything but keep a pointer/reference to A from within B.h, and do all real work with A from within B.cpp.
First of all - follow 'us2012' idea and use include guards!
Then - change the forward declarations:
A.h:
#ifndef A_H
#define A_H
class B;
class A{
A();
fooA();
...
private:
B b;
...
}
#endif
and the include B.h in A.cpp
in B.h you include again A.h - but the include guards should prevent errors:
#ifndef B_H
#define B_H
#include "A.h"
class B{
B()
B(A* const t)
fooB();
A* a; //pointer to A object
}
#endif
I haven't tested it ... but it should work.
SOLVED
What really helped me was that I could #include headers in the .cpp file with out causing the redefined error.
I'm new to C++ but I have some programming experience in C# and Java so I could be missing something basic that's unique to C++.
The problem is that I don't really know what's wrong, I will paste some code to try to explain the issue.
I have three Classes, GameEvents, Physics and GameObject. I have headers for each of them.
GameEvents has one Physics and a list of GameObjects.
Physics has a list of GameObjects.
What I'm trying to achieve is that I want GameObject to be able to access or own a Physics object.
If I simply #include "Physics.h" in GameObject I get the
"error C2111: 'ClassXXX' : 'class' type redifinition" which I understand.
And this is where I thought #include-guards would help so I added an include guard to my Physics.h since that's the header I want to include twice.
This is how it looks
#ifndef PHYSICS_H
#define PHYSICS_H
#include "GameObject.h"
#include <list>
class Physics
{
private:
double gravity;
list<GameObject*> objects;
list<GameObject*>::iterator i;
public:
Physics(void);
void ApplyPhysics(GameObject*);
void UpdatePhysics(int);
bool RectangleIntersect(SDL_Rect, SDL_Rect);
Vector2X CheckCollisions(Vector2X, GameObject*);
};
#endif // PHYSICS_H
But if I #include "Physics.h" in my GameObject.h now like this:
#include "Texture2D.h"
#include "Vector2X.h"
#include <SDL.h>
#include "Physics.h"
class GameObject
{
private:
SDL_Rect collisionBox;
public:
Texture2D texture;
Vector2X position;
double gravityForce;
int weight;
bool isOnGround;
GameObject(void);
GameObject(Texture2D, Vector2X, int);
void UpdateObject(int);
void Draw(SDL_Surface*);
void SetPosition(Vector2X);
SDL_Rect GetCollisionBox();
};
I get multiple issues that don't understand why they're showing up.
If I don't #include "Physics.h" my code runs just fine.
I'm very grateful for any help.
The preprocessor is a program that takes your program, makes some changes (for example include files (#include), macro expansion (#define), and basically everything that starts with #) and gives the "clean" result to the compiler.
The preprocessor works like this when it sees #include:
When you write:
#include "some_file"
The contents of some_file almost literally get copy pasted into the file including it. Now if you have:
a.h:
class A { int a; };
And:
b.h:
#include "a.h"
class B { int b; };
And:
main.cpp:
#include "a.h"
#include "b.h"
You get:
main.cpp:
class A { int a; }; // From #include "a.h"
class A { int a; }; // From #include "b.h"
class B { int b; }; // From #include "b.h"
Now you can see how A is redefined.
When you write guards, they become like this:
a.h:
#ifndef A_H
#define A_H
class A { int a; };
#endif
b.h:
#ifndef B_H
#define B_H
#include "a.h"
class B { int b; };
#endif
So now let's look at how #includes in main would be expanded (this is exactly, like the previous case: copy-paste)
main.cpp:
// From #include "a.h"
#ifndef A_H
#define A_H
class A { int a; };
#endif
// From #include "b.h"
#ifndef B_H
#define B_H
#ifndef A_H // From
#define A_H // #include "a.h"
class A { int a; }; // inside
#endif // "b.h"
class B { int b; };
#endif
Now let's follow the preprocessor and see what "real" code comes out of this. I will go line by line:
// From #include "a.h"
Comment. Ignore! Continue:
#ifndef A_H
Is A_H defined? No! Then continue:
#define A_H
Ok now A_H is defined. Continue:
class A { int a; };
This is not something for preprocessor, so just leave it be. Continue:
#endif
The previous if finished here. Continue:
// From #include "b.h"
Comment. Ignore! Continue:
#ifndef B_H
Is B_H defined? No! Then continue:
#define B_H
Ok now B_H is defined. Continue:
#ifndef A_H // From
Is A_H defined? YES! Then ignore until corresponding #endif:
#define A_H // #include "a.h"
Ignore
class A { int a; }; // inside
Ignore
#endif // "b.h"
The previous if finished here. Continue:
class B { int b; };
This is not something for preprocessor, so just leave it be. Continue:
#endif
The previous if finished here.
That is, after the preprocessor is done with the file, this is what the compiler sees:
main.cpp
class A { int a; };
class B { int b; };
So as you can see, anything that can get #included in the same file twice, whether directly or indirectly needs to be guarded. Since .h files are always very likely to be included twice, it is good if you guard ALL your .h files.
P.S. Note that you also have circular #includes. Imagine the preprocessor copy-pasting the code of Physics.h into GameObject.h which sees there is an #include "GameObject.h" which means copy GameObject.h into itself. When you copy, you again get #include "Pysics.h" and you are stuck in a loop forever. Compilers prevent that, but that means your #includes are half-done.
Before saying how to fix this, you should know another thing.
If you have:
#include "b.h"
class A
{
B b;
};
Then the compiler needs to know everything about b, most importantly, what variables it has etc so that it would know how many bytes it should put in place of b in A.
However, if you have:
class A
{
B *b;
};
Then the compiler doesn't really need to know anything about B (since pointers, regardless of the type have the same size). The only thing it needs to know about B is that it exists!
So you do something called "forward declaration":
class B; // This line just says B exists
class A
{
B *b;
};
This is very similar to many other things you do in header files such as:
int function(int x); // This is forward declaration
class A
{
public:
void do_something(); // This is forward declaration
}
You have circular references here: Physics.h includes GameObject.h which includes Physics.h. Your class Physics uses GameObject* (pointer) type so you don't need to include GameObject.h in Physics.h but just use forward declaration - instead of
#include "GameObject.h"
put
class GameObject;
Furthermore, put guards in each header file.
The issue is that your GameObject.h does not have guards, so when you #include "GameObject.h" in Physics.h it gets included when GameObject.h includes Physics.h.
Add include guards in all your *.h or *.hh header files (unless you have specific reasons to not do that).
To understand what is happening, try to get the preprocessed form of your source code. With GCC, it is something like g++ -Wall -C -E yourcode.cc > yourcode.i (I have no idea on how Microsoft compilers do that). You can also ask which files are included, with GCC as g++ -Wall -H -c yourcode.cc
Firstly you need include guards on gameobject too, but that's not the real problem here
If something else includes physics.h first, physics.h includes gameobject.h, you get something like this:
class GameObject {
...
};
#include physics.h
class Physics {
...
};
and the #include physics.h gets discarded because of the include guards, and you end up with a declaration of GameObject before the declaration of Physics.
But that's a problem if you want GameObject to have a pointer to a Physics, because for htat physics would have to be declared first.
To resolve the cycle, you can forward-declare a class instead, but only if you are just using it as a pointer or a reference in the declaration following, i.e.:
#ifndef PHYSICS_H
#define PHYSICS_H
// no need for this now #include "GameObject.h"
#include <list>
class GameObject;
class Physics
{
private:
list<GameObject*> objects;
list<GameObject*>::iterator i;
public:
void ApplyPhysics(GameObject*);
Vector2X CheckCollisions(Vector2X, GameObject*);
};
#endif // PHYSICS_H
Use include guards in ALL your header files. Since you are using Visual Studio you could use the #pragma once as the first preprocessor definition in all your headers.
However I suggest to use the classical approach:
#ifndef CLASS_NAME_H_
#define CLASS_NAME_H_
// Header code here
#endif //CLASS_NAME_H_
Second read about forward declaration and apply it.
The goal of a header guard is to avoid including the same file many times.
But the header guard that is currently used in C ++ can be improved. The current guard is:
#ifndef AAA_H
#define AAA_H
class AAA
{ /* ... */ };
#endif
My new guard proposal is:
#ifndef AAA_H
#define AAA_H
class AAA
{ /* ... */ };
#else
class AAA; // Forward declaration
#endif
This resolves the annoying problem that occurs when the AAA class needs the BBB class declaration, while the BBB class needs the AAA class declaration, typically because there are crossed pointers from one class to the other:
// File AAA.h
#ifndef AAA_H
#define AAA_H
#include "BBB.h"
class AAA
{
BBB *bbb;
/* ... */
};
#else
class AAA; // Forward declaration
#endif
//+++++++++++++++++++++++++++++++++++++++
// File BBB.h
#ifndef BBB_H
#define BBB_H
#include "AAA.h"
class BBB
{
AAA *aaa;
/* ... */
};
#else
class BBB; // Forward declaration
#endif
I would love for this to be included in the IDEs that automatically generate code from templates.
" #pragma once " ::: serves the same purpose as header guards, and has the added benefit of being shorter and less error-prone.
Many compilers support a simpler, alternate form of header guards using the #pragma directive:
" #pragma once "
// your code here
However, #pragma once is not an official part of the C++ language, and not all compilers support it (although most modern compilers do).
For compatibility purposes, people recommend sticking to traditional header guards. They aren’t much more work and they’re guaranteed to be supported on all compliant compilers.