C++构造函数:数字常量之前的预期标识符
C++ constructor: expected identifier before numeric constant
我正在尝试使用 SDL 用 c++ 编写游戏,但遇到了一个问题。我有一个蝙蝠课和一个游戏课。当我尝试创建一个 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
蝙蝠.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.
};
??
如果您尝试创建bat
初始化的成员变量0,0
请尝试以下操作:
class Game
{
public:
Game();
private:
Bat bat;
};
Game::Game() : bat(0, 0){
}
相关文章:
- "error: no matching function for call to"构造函数错误
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 选择要调用的构造函数
- 如何委托派生类使用其父构造函数?
- 构造函数正在调用一个使用当前类类型的函数
- 没有用于初始化C++中的变量模板的匹配构造函数
- 初始化具有非默认构造函数的std::数组项的更好方法
- 当从函数参数中的临时值调用复制构造函数时
- 在c++构造函数中使用随机字符串生成器
- C 如何从构造函数中声明(错误C2065未宣布的标识符)
- C++ 多态构造函数错误;标识符未定义
- C++构造函数:数字常量之前的预期标识符
- 构造函数错误:需要标识符
- 构造函数初始值设定项列表中的 Lambda 可以使用哪些标识符
- 名称空间作用域构造函数定义是否需要类限定的标识符
- 创建的对象总是未定义,并且在初始化构造函数时期望有标识符
- 默认构造函数中未识别的标识符