2

OK、これは私を困惑させています。比較的 C++ 初心者ですが、C# やその他の言語での長い経験があります。

問題のあるファイルを比較的簡単に要約すると、次のようになります。

/* 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 個のまったく無関係なエラーが生成されます。これは、コンポーネントをコンパイルできなかったという事実に起因していると思います。

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

何か案は?Component が GameObject へのポインターを持ち、GameObject が Components へのポインターのベクトルを持つことは設計上意味があるので、それを避けるために再構築するつもりはありません。ヘッダーファイルに何か問題があるだけだと思います。

アイデアをお寄せいただきありがとうございます。これは私を夢中にさせています。

4

2 に答える 2

2

これを修正する必要があるのは、前方宣言を追加することだけです - GameObject 定義の前にコンポーネントを追加し、その逆も同様です

class GameObject;
class Component {
...

class Component;
class GameObject{
...

技術的には、.h ファイルを注文する方法のために、2 番目のファイルが必要です。ただし、両方を追加することをお勧めします。

この理由は、あなたの.hファイルを独立した C++ ファイルと考えると、(コンパイラーが) ポインターのベクトルの定義に遭遇するまでにComponent(なぜこれがベクトルへのポインターなのか??)、まだ何もないからです。何であるかを考えComponentます。それは、クラス、関数、タイプミスなど、何でもかまいません。これが、クラスであると想定することをコンパイラに知らせるために前方宣言が必要な理由です。

これは、他のクラスへのポインター/参照の場合にのみ機能します。オブジェクトのベクトルである場合はComponent、定義の前にヘッダーを含めるしかありませんでした。

于 2012-12-06T01:43:48.607 に答える
1

#pragma once の後に Component の前方宣言を先頭に置きます。

class Component; // Just this, no more.

まだエラーがあるかもしれませんが、それが始まりです。

GameObject.h と Component.h を 1 つのファイルに結合することをお勧めします。それらは密接に関連しているため、一緒に属しています。

于 2012-12-06T01:39:24.570 に答える