好的,這是困擾我。相對C++ noob,但長期使用C#和其他語言的經驗。與引用對方的C++頭文件的神祕編譯器錯誤
這裏的問題文件的一個相對簡單的蒸餾:
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
:
/* 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() {
}
這生成在Visual C++ 2012 21級完全無關的錯誤,我想從一個事實,即它無法編譯組件所產生
有沒有想法?在Component的設計中有一個指向GameObject的指針,而GameObject有一個指向組件的指針矢量是有意義的,所以我不打算重新構建以避免這種情況。我猜我只是做了頭文件錯誤的事情。
在此先感謝您的任何建議,這個讓我瘋狂。
[在C++中包含對方的頭文件可能重複](http://stackoverflow.com/questions/396084/headers-including-each-other-in-c) – godel9