0
这里的第一个问题,答案可能很简单,但我无法弄清楚。点: 在我的项目中,我创建了2个类:“GlobalVairables”和“SDLFunctions”。 显然,在第一个我想存储我可以涉及任何其他类的全局变量,并在第二个我使用这些全局变量的函数。这里是代码:全局变量类C++
GlobalVariables.h
#pragma once
class GlobalVariables
{
public:
GlobalVariables(void);
~GlobalVariables(void);
const int SCREEN_WIDTH;
const int SCREEN_HEIGHT;
//The window we'll be rendering to
SDL_Window* gWindow;
//The surface contained by the window
SDL_Surface* gScreenSurface;
//The image we will load and show on the screen
SDL_Surface* gHelloWorld;
};
和GlobalVariables.cpp
#include "GlobalVariables.h"
GlobalVariables::GlobalVariables(void)
{
const int GlobalVairables::SCREEN_WIDTH = 640;
const int GlobalVariables::SCREEN_HEIGHT = 480;
SDL_Window GlobalVairables:: gWindow = NULL;
SDL_Surface GlobalVariables:: gScreenSurface = NULL;
SDL_Surface GlobalVariables:: gHelloWorld = NULL;
}
GlobalVariables::~GlobalVariables(void)
{
}
,这里是在SDLFunction.cpp一个功能,即使用 “gWindow” 等2个变量:
gWindow = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
我的问题是,在调试时,我得到
error C2065: 'gWindow' : undeclared indentifier
当然,在SDLFunctions.cpp中我得到了“#include”GlobalVariables.h“”。另外,这些变量是公开的,所以它不是(可能)。 有人可以说出了什么问题吗?有一些简单的解决方案,还是我必须重新组织它,而不应该使用全局变量?请帮忙。
如何是那些变量的全局?如果将整行更改为'gWindow = null;'它应该起作用。 – forkrul
您似乎没有创建该类的实例(可能应该是单例类或命名空间),而且您似乎错误地引用了标识符('GlobalVariables :: gWindow'而不是'gWindow' ) – UnholySheep
@UnholySheep - 这就是Java的习惯用法:除非没有,否则一切都是对象。你绝对正确的,这应该是一个静态数据成员而不是类的命名空间。 –