当我将以下内容声明为我的主类" App"的私有数据成员时,我的程序在启动时崩溃,好像它正在访问无效的数据/内存(编辑:它没有崩溃如果是公共数据成员):
//Display position and dimensions of the entire app
SDL_Rect DisplayRect;
如果我删除它,程序启动很好,但我真的不明白为什么。我验证了以下内容:
它没有任何意义,我在其他课程中拥有私人SDL_Rect数据成员,并且他们不会导致任何问题。我认为它是关于初始化的东西,但我不明白为什么在这种情况下我应该做任何特别的事情。以下是我的App类的所有数据成员和初始化列表:
/* Private data members */
private:
//Whether or not the app loop is running
bool running;
//The starting time of the program, starting when the App object is created
clock_t start_time;
//Variable holding count of loops executed after the App object entered its main loop - use the defined public method
//to increase the count at the proper location inside the App object's main loop
long long int loop_count;
//SDL Window pointer to hold the main application window
SDL_Window* MainWindow;
//Event Manager object to check and manage main application events
EventManager MainEvents;
//SDL renderer pointer to perform rendering operations
SDL_Renderer* MainRenderer;
//App dependencies structure to be passed to lower level objects for dependency injection
AppDependencies MainDependencies;
//Room object holding data for the current room into which the App is running - initialize with default values
Room CurrentRoom;
//Display position and dimensions of the entire app
SDL_Rect DisplayRect;
/* Public methods */
public:
//Constructor - Initialization list
App() :
MainDependencies(this->MainWindow, &this->MainEvents, this->MainRenderer),
CurrentRoom(&this->MainDependencies, RoomId::SPLASH_SCREEN, 1024, 768, 1024, 768)
{
//Set data member telling that the app is running
this->running = true;
//Get the starting time of the App object
this->start_time = clock();
//Set loop count to 0
this->loop_count = 0;
//Set MainWindow pointer to null
MainWindow = nullptr;
//Set MainRenderer pointer to null
MainRenderer = nullptr;
}
答案 0 :(得分:0)
解决方案是我必须在初始化列表中初始化两个对象之前声明我的SDL_Rect。
我不知道这样的声明如此重要,希望它能在未来为人们提供帮助!