2016-07-26 4 views
2

これはコンパイルはなぜクラスの仕事のフォワード宣言はクラスが別のクラスには含まれていない場合

#include "Sprite.h" 

class GameObject 
{ 
    public: 
     int x, y, w, h; 
     Sprite sprite; 
    public: 
    GameObject(); 
    GameObject(int _x, int _y, int _w, int _h); 
    virtual ~GameObject(); 
}; 

これがないの

class Sprite; 

class GameObject 
{ 
    public: 
     int x, y, w, h; 
     Sprite sprite; 
    public: 
    GameObject(); 
    GameObject(int _x, int _y, int _w, int _h); 
    virtual ~GameObject(); 
}; 

私は宣言し、ためにポインタを使用転送することができることを知っていますスプライトだが、ここでは宣言を働かせない理由はここにある。 "クラススプライト;"スプライトが存在することを伝えますか? 私は、.cppで#includeクラスを#includeしようとしています。 また、クラスは互いに含まれていないので、Sprite *を使用する必要はありません。 私は、前方宣言が何であるかについての私の理解は間違っていると思います。なぜなら、これはうまくいかない理由がないからです。

ありがとうございます。

答えて

3

あなたがコンパイラであるとふりまえてください。 Spriteを完全に宣言することなく、Spriteが1バイトだけ大きいのか、それとも10万バイト大きいのかをどのように判断できますか?

クラスへのポインタ(またはクラスへの参照、その他の小さなもの)が必要な場合は、クラスについて多くのことを知る必要はありません。実際にクラスを使用する必要がある場合は、単なる前方宣言では不十分です。 "Spriteが存在する"ことを知ることは必ずしも十分ではありません。時にはそれがどれだけ大きいかを知る必要があることもあります。完全な宣言がなければ、それは不可能です。

+0

ポインタで動作する理由は、ポインタが知る必要があるのはメモリアドレスのサイズが常に同じであるためです。 –

+0

@DrunkBearzzはい! –

+0

クール、それはそれを要約する、私はまた、ポインタについて、何かを学ぶ –

2

前方宣言は、依存するクラス宣言に型が含まれている場合、参照またはポインタでのみ機能します。

class Sprite; 

class GameObject 
{ 
    public: 
     int x, y, w, h; 
     Sprite* sprite; // <<<< 
    // or Sprite& sprite; 
    public: 
    GameObject(); 
    GameObject(int _x, int _y, int _w, int _h); 
    virtual ~GameObject(); 
}; 

ケアは、あなたの実装でSprite.hファイルを含めて、理想的にあなたのコンストラクタの実装でメンバーを初期化する(参照は、厳密にそれを必要とします)。

+0

私はそれを今得ました、ありがとう。 –

1

Spriteincomplete typeのサイズとレイアウトは、GameObjectの非静的メンバーとして知られている必要があるため、ここでは使用できません。

(第3回1注)

次のコンテキストのいずれかが完全であることをクラスTが必要です。一方

definition or function call to a function with return type T or argument type T; 
definition of an object of type T; 
declaration of a non-static class data member of type T; 
new-expression for an object of type T or an array whose element type is T; 
lvalue-to-rvalue conversion applied to a glvalue of type T; 
an implicit or explicit conversion to type T; 
a standard conversion, dynamic_cast, or static_cast to type T* or T&, except when converting from the null pointer constant or from a pointer to void; 
class member access operator applied to an expression of type T; 
typeid, sizeof, or alignof operator applied to type T; 
arithmetic operator applied to a pointer to T; 
definition of a class with base class T; 
assignment to an lvalue of type T; 
a catch-clause for an exception of type T, T&, or T*. 

、あなたはポインタまたは参照としてそれを宣言した場合、不完全な型はうまくいき、前方宣言が許可されます。

関連する問題