2016-06-14 7 views
0

SDLでC++でゲームを作成しようとしていますが、問題が発生しました。 私はBatクラスとGameクラスを持っています。 私はバットオブジェクトを作成し、私は次のエラーを取得するコンストラクタを呼び出すようにしようとしている:C++コンストラクタ:数値定数の前に識別子がありません

"error: expected identifier before numeric constant"

ここでは、ソースファイルは、次のとおりです。

Game.h

#ifndef GAME_H 
#define GAME_H 

#include "SDL.h" 
#include "Bat.h" 

class Game 
{ 
    public: 
     Game(); 
     Bat bat(0, 0); 
    private: 
}; 

#endif // GAME_H 

Bat.h

#ifndef BAT_H 
#define BAT_H 

class Bat 
{ 
    public: 
     Bat(int x, int y); 
     int getX() {return x;} 
     int getY() {return y;} 
    private: 
     int x, y; 
}; 

#endif // BAT_H 

Bat.cpp

#include "Bat.h" 

Bat::Bat(int x, int y) 
{ 
} 
+1

を。 'Game'コンストラクタのような関数でそれを行う必要があります。そして、あなたは '' Bat'コンストラクタに渡している 'x'と' y'を保存していません。 – Frecklefoot

答えて

2

あなたは

class Game 
{ 
    public: 
     Game() : bat(0, 0) {} // <<< or move that definition to your .cpp file 
    private: 
     Bat bat; // << you can't initialize the member here. 
}; 

を書くこともしかして?

+1

はい、ありがとうございます。それを解決しました –

1

あなたは0,0で初期化されたメンバ変数batを作成しようとしている場合は、この試してください:あなたは、クラス宣言で `Bat`オブジェクトを割り当てることができない

class Game 
{ 
    public: 
     Game(); 

    private: 
     Bat bat; 
}; 

Game::Game() : bat(0, 0){ 
} 
+0

はい、私はそれを解決しました、ありがとう! –

関連する問題