0

循環依存の問題があります。2 つのヘッダー ファイルがあり、それぞれが相互に依存しています。私が抱えている問題は、名前空間内のクラスに関係しています。

ファイル #1

class Player; // This is how I forward declare another class

namespace sef {

class Base
{
   public:
   Player a;
   bool SendEvent(int eventType);
};

class Sub: public Base
{
    protected:
    Player b;
    bool Execute(string a);
};
}

ファイル #2

//class sef::Sub; // I am having trouble compiling this

class Player
{
   public:
      sef::Sub* engine; // I am having trouble compiling this
};

ファイル #2 で sef::Sub クラスを前方宣言するにはどうすればよいですか?

4

1 に答える 1

1

まず、型のみを宣言すると、ポインタまたは参照しか使用できません。

class Player; // declaration, not definition
class Base {
  Player* p;
};

次に、名前空間は拡張可能であるため、次のように記述できます。

namespace Foo { class Player; }

そしてポインタを使用します:

class Base {
  Foo::Player* p;
}
于 2014-02-20T23:33:18.890 に答える