Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/162.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++;构造函数:数字常量之前应为标识符 我尝试用SDL写一个C++游戏,我遇到了一个问题。 我有一节蝙蝠课和一节游戏课。 当我尝试创建bat对象并调用构造函数时,会出现以下错误:_C++ - Fatal编程技术网

C++;构造函数:数字常量之前应为标识符 我尝试用SDL写一个C++游戏,我遇到了一个问题。 我有一节蝙蝠课和一节游戏课。 当我尝试创建bat对象并调用构造函数时,会出现以下错误:

C++;构造函数:数字常量之前应为标识符 我尝试用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:

“错误:数字常量前应有标识符”

以下是源文件:

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:
        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
。是的,谢谢。这解决了问题是的,我解决了,谢谢!