C++ 构造函数 : expected identifier before numeric constant

标签 c++

<分区>

我正在尝试使用 C++ 和 SDL 编写游戏,但我遇到了一个问题。 我有一个 Bat 类和一个 Game 类。 当我尝试创建一个 bat 对象并调用构造函数时,出现以下错误:

"error: expected identifier before numeric constant"

这里是源文件:

游戏.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)
{
}

最佳答案

你是不是想写

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.
};

??

关于C++ 构造函数 : expected identifier before numeric constant,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37820082/

相关文章:

c++ - 使用QNetworkAccessManager Investing.com的网站出现问题

c++ - 内联函数和 __thiscall __cdecl Bjarne String 示例

python - 计算大型稀疏矩阵的迹

c++ - 从类模板创建对象的工厂函数模板?

c++ - Nvidia Nsight Eclipse 版使用 C/C++ 远程启动配置进行远程调试

C++重新排列一个整数

c++ - 线性规划 : Modulo constraint

c++ - 高效地使用函数输出

C++Builder 使用 TCP 发送数组

c++ - 购物计算器