OK, this is stumping me. Relative C++ noob, but long experience with C# and other languages.
Here’s a relatively simple distillation of the problem files:
/* GameObject.h */
#pragma once
#include <vector>
class GameObject {
public:
std::vector<Component *> *Components;
GameObject();
~GameObject();
};
/* GameObject.cpp */
#include "GameObject.h"
#include "Component.h"
GameObject::GameObject() {
}
GameObject::~GameObject() {
}
/* Component.h */
#pragma once
class Component {
public:
GameObject *Owner;
Component();
~Component();
};
/* Component.cpp */
#include "GameObject.h"
#include "Component.h"
Component::Component() {
}
Component::~Component() {
}
This generates 21 totally irrelevant errors in Visual C++ 2012, I guess stemming from the fact that it couldn’t compile Component:
C2065: 'Component' : undeclared identifier gameobject.h 10
C2059: syntax error : '>' gameobject.h 10
C2143: syntax error : missing ';' before '}' gameobject.h 14
C2143: syntax error : missing ';' before '{' component.h 3
C2143: syntax error : missing ';' before '}' component.h 11
C2143: syntax error : missing ';' before '{' gameobject.cpp 8
C2143: syntax error : missing ';' before '}' gameobject.cpp 9
C2143: syntax error : missing ';' before '{' gameobject.cpp 13
C2143: syntax error : missing ';' before '}' gameobject.cpp 14
C2143: syntax error : missing ';' before '}' gameobject.cpp 16
C1004: unexpected end-of-file found gameobject.cpp 16
C2065: 'Component' : undeclared identifier gameobject.h 10
C2059: syntax error : '>' gameobject.h 10
C2143: syntax error : missing ';' before '}' gameobject.h 14
C2143: syntax error : missing ';' before '{' component.h 3
C2143: syntax error : missing ';' before '}' component.h 11
C2653: 'Component' : is not a class or namespace name component.cpp 8
C2143: syntax error : missing ';' before '{' component.cpp 8
C2143: syntax error : missing ';' before '}' component.cpp 9
C2653: 'Component' : is not a class or namespace name component.cpp 13
C1903: unable to recover from previous error(s); stopping compilation component.cpp 13
Any ideas? It makes sense in the design for Component to have a pointer to GameObject, and GameObject to have a vector of pointers to Components, so I’m not about to rearchitect to avoid that. I’m guessing I’m just doing something wrong with the header files.
Thanks in advance for any ideas, this one’s driving me crazy.
All you need to fix this is to add forward declarations – Component before GameObject definition and vice versa
and
Technically you just need the 2nd one because of the way you order your .h files. But it is better you add both.
The reason for this is because if we think of your
.has independant C++ files, by the time we(the compiler) come across the definition of the vector ofComponentpointers (why is this a pointer to a vector??), we still have no idea whatComponentis. It could be a class, a function, a typo, anything. This is why you need a forward declaration to let the compiler know to assume it is a class.This would work only in the case of pointers/references to other classes. If it were a vector of
Componentobjects, you would have had no choice but to include the header before the definition.