【发布时间】:2015-11-04 16:09:49
【问题描述】:
我正在用 C++ 制作游戏。我还没有开始编写游戏,我正在设置不同的类并制作菜单。这是我第一次制作“大”程序,我发现自己将所有内容都设置为静态。当我将类中的所有内容设为静态时,出于某种原因,我需要将变量设为 const
error C2864: 'GameWindow::ScreenHeight': a static data member with an in-class initializer must have non-volatile const integral type
当我将它们设为 const 时,我得到另一个错误:
error C3892: 'ScreenHeight': you cannot assign to a variable that is const
这是我的 GameWindow 类:
class GameWindow {
public:
static sf::RenderWindow mainWindow;
static void SetScreenWidth(int x);
static int GetScreenWidth();
static void SetScreenHeight(int x);
static int GetScreenHeight();
static void Initialize();
private:
static const int ScreenWidth = 1024;
static const int ScreenHeight = 576;
};
由于某种原因我不能这样做
void GameWindow::SetScreenHeight(int x) {
ScreenHeight = x;
}
我知道导致问题的原因 - 我无法更改 const 整数的值 - 但我不知道如何解决。
【问题讨论】:
-
const = 常量 = 不可变 = 无法更改。顺便说一句,每个 GameWindow 实例不应该有自己的宽度和高度吗?
-
你为什么要让所有这些成员
static? -
为什么要在 GameWindow 类中管理屏幕上的信息?你不应该有一个单例 Screen 类吗?
-
@i4h 正如我所说,这是我的第一个大型程序,所以请原谅我做错了一切。你需要从某个地方开始,对吧?我修复了错误,但现在我有 9 个“未解决的外部符号”错误。它们与库或文件有关,我找不到修复它的方法。
-
@bames53 我需要你的帮助 ^