gpt4 book ai didi

c++ - 声明 SDL_Rect 数据成员在启动时崩溃,找不到原因

转载 作者:行者123 更新时间:2023-12-01 19:15:47 27 4
gpt4 key购买 nike

当我将以下内容声明为我的主类“App”的私有(private)数据成员时,我的程序在启动时崩溃,就好像它正在访问无效数据/内存一样(编辑:如果它是公共(public)数据成员,它不会崩溃):

//Display position and dimensions of the entire app
SDL_Rect DisplayRect;

如果我删除它,程序启动正常,但我真的不明白为什么。我验证了以下几点:

  • SDL 库已正确初始化
  • 包含“SDL_Rect” header
  • 编译器不会抛出警告或错误

这没有任何意义,而且我在其他类中有私有(private) 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;
}

最佳答案

解决方案是我必须在初始化列表中初始化的两个对象之前声明 SDL_Rect。

我不知道声明订购有这么重要,希望它可以帮助以后的人!

关于c++ - 声明 SDL_Rect 数据成员在启动时崩溃,找不到原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25341368/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com