C++ Tricky Inheritance Class Definition Problem - c++

I'm getting this error when dealing with a number of classes including each other:
error: expected class-name before '{' token
I see what is going on, but I do not know how to properly correct it. Here is an abstracted version of the code:
A.h
#ifndef A_H_
#define A_H_
#include "K.h"
class A
{
public:
A();
};
#endif /*A_H_*/
A.cpp
#include "A.h"
A::A() {}
B.h
#ifndef B_H_
#define B_H_
#include "A.h"
class B : public A
{ // error: expected class-name before '{' token
public:
B();
};
#endif /*B_H_*/
B.cpp
#include "B.h"
B::B() : A() {}
J.h
#ifndef J_H_
#define J_H_
#include "B.h"
class J
{
public:
J();
};
#endif /*J_H_*/
J.cpp
#include "J.h"
J::J() {}
K.h
#ifndef K_H_
#define K_H_
#include "J.h"
class K : public J
{ // error: expected class-name before '{' token
public:
K();
};
#endif /*K_H_*/
K.cpp
#include "K.h"
K::K() : J() {}
main.cpp
#include "A.h"
int main()
{
return 0;
}
Starting in main.cpp, I can determine that this is what the compiler sees:
#include "A.h"
#ifndef A_H_
#define A_H_
#include "K.h"
#ifndef K_H_
#define K_H_
#include "J.h"
#ifndef J_H_
#define J_H_
#include "B.h"
#ifndef B_H_
#define B_H_
#include "A.h"
class B : public A
{ // error: expected class-name before '{' token
So, A's definition is not complete when we get to B. I've been told that sometimes you need to use a forward declaration and then move the #include statement into the .cpp file, but I'm not having any luck with that. If I try anything like that, I simply get the additional error:
error: forward declaration of 'struct ClassName'
I think maybe I'm just not doing things in the right places. Can someone please show me how to get this code to compile? Thank you very much!
Edit: I want to point out that this is just abstracted version of the real code. I realize that there are no references to K in A or B in J, but there are in the real code and I feel that they're completely necessary. Perhaps if I give a brief description of the real classes, someone can help me restructure or fix my code.
Class A is an abstract node class that acts as an interface for nodes in a graph. Class B is one of what will be a number of different implementations of A. In the same manner, class J is an abstract Visitor class and K is the corresponding implementation. Here is the code with a little more context:
A.h (Abstract Node)
#ifndef A_H_
#define A_H_
#include "K.h"
class K;
class A
{
public:
A();
virtual void accept(const K&) const = 0;
};
#endif /*A_H_*/
A.cpp
#include "A.h"
A::A() {}
B.h (Concrete Node)
#ifndef B_H_
#define B_H_
#include "A.h"
class K;
class B : public A
{ // error: expected class-name before '{' token
public:
B();
virtual void accept(const K&) const;
};
#endif /*B_H_*/
B.cpp
#include "B.h"
B::B() : A() {}
void B::accept(const K& k) const { k.visit(this); }
J.h (Abstract Visitor)
#ifndef J_H_
#define J_H_
#include "B.h"
class B;
class J
{
public:
J();
virtual void visit(const B*) const = 0;
};
#endif /*J_H_*/
J.cpp
#include "J.h"
J::J() {}
K.h (Concrete Visitor)
#ifndef K_H_
#define K_H_
#include "J.h"
class B;
class K : public J
{ // error: expected class-name before '{' token
public:
K();
virtual void visit(const B*) const;
};
#endif /*K_H_*/
K.cpp
#include "K.h"
K::K() : J() {}
void K::visit(const B*) const {};
main.cpp
#include "A.h"
int main()
{
return 0;
}
I had to add some forward declarations to make some additional errors that appeared (when I added detail) go away. Some of them may not be necessary or correct.

Your header inclusions are circular. A -> K -> J -> B -> A. Using forward declarations is the simplest way to avoid such complexity, but it is only possible when the class being declared only uses references or pointers to the class being included. You can't use forward declaration in K.h or B.h, for example, because they inherit from J and A respectively. However, you may be able to replace the #include "K.h" in A.h with class K; depending on how you are actually using K in A.
Talk about confusing.

In this concrete example, remove the
#include "B.h"
from file J.h since it' s not needed there. If that doesn't work, we would need some more details about how J uses B...
EDIT
Since J only uses a pointer to B, the advice stays the same :-) :
Remove #include "B.h" from J.h and replace it by a forward declaration of B:
(J.h)
class B;
class J
{
// ...
virtual void visit(const B*) const = 0; // This will work with forward declaration
}
Also remove #include "K.h"from A.h.
IMPORTANT
Of course you need to add the needed includes in the respective CPP files:
(J.cpp)
#include "B.h"
// ...
// Go ahead with implementation of J
// ...
(Same for A.cpp, include K.h)

The main problem is that your header files include each other in a circular manner. A includes K which includes J which includes B which then includes A again... You should never require a situation where this happens. You should go back to the design drawing board and either cut some of the dependencies, or reorganize your classes entirely so that this circular dependency doesn't happen.

The problem is that your header files cyclically depend on each other. Don't include K.h in A.h and B.h in J.h. They aren't needed there.
Regarding your edit: this is not the best way for your objects to interact. Rethink your design. Who calls node.accept(visitor)? Can't you call visitor(node) directly?
Also, if you're trying to design a graph library, have a look at Boost.Graph library.
That said, since you only use a pointer to B in J.h, you don't have to include B.h, only forward-declare the class.
class B;
struct J
{
virtual void visit(const B*) const = 0;
};
Then include B.h in your K.cpp file.
#include "K.h"
#include "B.h"
void K::visit(const B* b) const
{
// use b here any way you want
}

Circular inclusions do not work.
Try to keep inclusions to a strict minimum. If you do that, you'll either be fine altogether, or you'll discover problems in your design. In your case, i don't see anything wrong with your design.
When defining class K, you're only using a pointer to an object of type B. That does not require B to be defined (as in "include the header file"), only to be declared (forward declaration is fine). So, in your case, removing inclusion to header "B.h" replaced by "class B;" is sufficient. (the same goes for class J)

Related

C++ pass struct from from one class to another class

I have two classes A and B defined in A.h, A.cpp and B.h, B.cpp respectively. Class A has a structure that I want to use in a function of Class B. Because Class B is included in Class A, I can't include class A in Class B as it will lead to circular dependency. Code for all the files are given below:
A.h
#ifndef _A_H
#define _A_H
#include B.h
namespace common {
class A {
public:
static struct strctOfA {
float p1 = 2;
} structA;
void functionOfA();
};
}
#endif // !_A_H
A.cpp
#include A.h
using namespace common;
A::functionOfA() {
B b;
b.functionOfB(structA);
}
B.h
#ifndef _B_H
#define _B_H
namespace common {
class B {
public:
functionOfB(??);
};
}
#endif // !_B_H
B.cpp
#include B.h
using namespace common;
B::functionOfB(??) {
// Want to use structA here;
}
I looked into StackOverflow and found a thread quite close to my problem, however, they it either doesn't explains the answer or I am not able to understand their solution. Please help me with this.
Thanks
Update
Thanks I-V for pointing out the mistake and providing me the explanation. Updating the code worked for me. Thanks everyone for providing additional knowledge to my C/C++ coding skills. :)
The solution is simple, what you wrote is a bad habit.
There is no reason to include B.h in A.h because you don't use any part of B.h in the A.h file. A better way to implement it is to include B.h in A.cpp and not in the header file.
It will also solve the cycle of includes you have..
In general, it is recommended to include files in .cpp file and NOT in header files when you don't use functions/objects of the included file in the header file :)
In addition, you should use #pragma once for Windows or ifndef for anything else in order to be safe of conflicts
A.h
#ifndef _A_H
#define _A_H
namespace common {
class A {
public:
static struct strctOfA {
float p1 = 2;
} structA;
}
}
#endif
A.cpp
#include A.h
#include B.h
using namespace common;
class A {
B b;
b.functionOfB(structA);
}
NOTE: from B.cpp include A.h
That can be done with forward-declarations, unless you abolutely must pass it by value. But I can't think of a scenario where this wouldn't work with references.
A.h
#ifndef A_H_
#define A_H_
struct B;
struct A {void f(B&);};
#endif
A.cpp
#include "B.h"
void A::f(B & value) {/* do something */ };
B.h
#ifndef B_H_
#define B_H_
struct A;
struct B {void f(B&);};
#endif
B.cpp
#include "A.h"
void B::f(A & value) {/* do something */ };
If you need to have a value of B in A with only a forward-declaration you can put it in a pointer, i.e. use a std::unique_ptr.

ELI5: What is a macro name? [duplicate]

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.

Using friend function in C++

Just read about friend functions and I'm trying to access private variable "number" in class A with friend function "Print" from class B. I'm working with Visual Studio. Compilation of my code gives me plenty of various errors like:
C2011: 'A' : 'class' type redefinition
C2653: 'B' : is not a class or namespace name
Please be patient with me me and show a proper way of achieving my goal.
Here are my files
A.h:
class A
{
public:
A(int a);
friend void B::Print(A &obj);
private:
int number;
};
A.cpp:
#include "A.h"
A::A(int a)
{
number=a;
}
B.h:
#include <iostream>
using namespace std;
#include "A.h"
class B
{
public:
B(void);
void Print(A &obj);
};
B.cpp:
#include "B.h"
B::B(void){}
void B::Print(A &obj)
{
cout<<obj.number<<endl;
}
main.cpp:
#include <iostream>
#include <conio.h>
#include "B.h"
#include "A.h"
void main()
{
A a_object(10);
B b_object;
b_object.Print(A &obj);
_getch();
}
... Second you might need a forward declaration of class B in the A.h header file to refer B as a friend:
#ifndef _A_H_
#define _A_H_
class B;
class A
{
friend class B;
};
#endif
UPDATE
I'm currently not so sure if it's possible to declare member functions as friends, I'll have a look.
It's not possible to create member function friend declarations, you can either declare global functions or whole classes as friend, see also: C++ ref, Friendship and inheritance.
In general it's not a good design idea to use friend at all, because it strongly couples the classes together. The better solution will be to couple interfaces (which don't need to be publicly visible anyways).
In rare cases it might be a good design decision but that almost always applies to internal details.
First you need to put
#ifndef A_H
#define A_H
.... your A-class definition
#endif
in your A.h and analogous in B.h, so that multiple includes of the same file is prevented (that is your duplicate definition error)...

C++ #include guards

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.

C++ Class instantiation problem

I have included the proper
Header Files ,
Header Gard
but i cannot instantiate a specific class
Getting Error
error C2065: 'ClassName' : undeclared identifier
Sample Code
Class A{
//instantiate class B
}
Class B {
//need to instantiate Class A
}
Since you haven't posted any real code for us to actually make use of, I'll take a guess as to what your code actually looks like:
A.h:
#ifndef HEADER_A
#define HEADER_A
#include "B.h"
class A {
private:
B someMember;
};
B.h:
#ifndef HEADER_B
#define HEADER_B
#include "A.h"
class B {
public:
doSomething(A param);
};
B.cpp:
#include "B.h"
#include "A.h"
void B::doSomething(A param) { }
As Flinsch said, you need to have forward declarations to avoid problems with include ordering. The simplest way is to get rid of the circular includes in the .h files and just include the forward declarations (class B; and class A; into A.h and B.h) instead.
btw. you don't have ; at the end of class definition.
Under my understanding there is a circular dependency between classes A and B. So you need to use forward declaration for at least one of those two classes (or even both).