如何在 SDL2 C++ 中修复 "Exception thrown: read access violation. **Surface** was nullptr. occurred"

How to fix "Exception thrown: read access violation. **Surface** was nullptr. occurred" in SDL2 C++

我正在尝试使用 C++ 学习 SDL2,

每次我尝试填充表面时都会出现此错误,在互联网上搜索解决方案但尚未找到:

SDL_Surface* Surface = NULL;
SDL_FillRect(Surface, NULL, SDL_MapRGB(Surface->format, 0, 255, 0));

每次我 运行 这样做时,它都不会显示错误,但它确实会破坏代码并在 FillRect 行附近显示一个 X 和一个带有文本 "Exception thrown: read access violation. Surface was nullptr." 的小框。我可以单击 "Copy Details",然后它会复制“抛出的异常:读取访问冲突。 ** Surface ** 是 nullptr。发生了

你需要使用像

这样的东西
SDL_CreateRGBSurface

而不是 NULL。因为你不能写入内存中不存在的东西。