C++;构造函数:数字常量之前应为标识符 我尝试用SDL写一个C++游戏,我遇到了一个问题。 我有一节蝙蝠课和一节游戏课。 当我尝试创建bat对象并调用构造函数时,会出现以下错误:
“错误:数字常量前应有标识符” 以下是源文件: Game.hC++;构造函数:数字常量之前应为标识符 我尝试用SDL写一个C++游戏,我遇到了一个问题。 我有一节蝙蝠课和一节游戏课。 当我尝试创建bat对象并调用构造函数时,会出现以下错误:,c++,C++,“错误:数字常量前应有标识符” 以下是源文件: 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 #ifndef BAT_H #define BAT_H class Bat { public:
#ifndef GAME_H
#define GAME_H
#include "SDL.h"
#include "Bat.h"
class Game
{
public:
Game();
Bat bat(0, 0);
private:
};
#endif // GAME_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.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
#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)
{
}
你是想写信吗
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.
};
类游戏
{
公众:
Game():bat(0,0){}/如果您试图创建一个成员变量bat
用0,0
初始化,请尝试以下操作:
class Game
{
public:
Game();
private:
Bat bat;
};
Game::Game() : bat(0, 0){
}
您不能在类声明中分配Bat
对象。您必须在函数中进行分配,如Game
构造函数。并且您没有保存要传递到Bat
构造函数的x
和y
。是的,谢谢。这解决了问题是的,我解决了,谢谢!