当我尝试定义结构的元素时,我不断收到错误消息
I keep getting an error when I try to define an element of a struct
我正在自学 C。我按照教程进行操作,得到了一个可以在屏幕上四处移动的图像。现在我正在尝试自己做并了解如何模块化我的代码并知道它发生了什么。
我构建了一个结构来获取玩家坐标并将其调用到我的 game_loop.h 文件中。但它不允许我从结构中设置变量。我试图只包括重要的部分以保持简洁。让我知道是否需要 post 整个代码。
我究竟做错了什么。
//includes
#include "game_loop.h"
//main body
int main( int argc, char *argv[])
{
//TODO make game menu and link it here
//TODO make game loop and put it here
initSDL();
renderGame();
handleEvent();
//make game cleanup and put it her
destroySDL();
return 0;
}
int头文件game_loop.h -->
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "player.h"
#define pSIZE 64
#define wWIDTH 1280
#define wHEIGHT 720
//variables for starting SDL
SDL_Event event;
SDL_Window *window = NULL;
SDL_Renderer *render = NULL;
SDL_Surface *bgSurface = NULL;
SDL_Texture *bgTexture = NULL;
SDL_Surface *pSurface = NULL;
SDL_Texture *pTexture = NULL;
int flags = 0; //window flags may need to change in the future
struct Player player;
player.x = 600;
player.y = 300;
void initSDL()
{
//initializing SDL
if(SDL_Init(SDL_INIT_VIDEO)!= 0)
{
printf("ERROR starting SDL: %s\n", SDL_GetError());
}else{printf("Starting SDL: Successful.\n");}
在 player.h 文件中 -->
struct Player{
int x;
int y;
};
您在函数之外有几行可执行代码:
Player player;
player.x = 600;
player.y = 300;
第一行定义了一个变量。那没问题。接下来的两行不是,因为它们是语句。
您需要在定义结构时对其进行初始化。您可以按如下方式进行:
Player player = { 600, 300 };
此外,在 header 文件中定义变量也不是一个好主意。如果在多个源文件中使用 header,您最终会因多个定义而出错。
在您的 header 文件中,变量应声明为 extern
而无需初始化程序:
extern Player player;
然后你会把带有初始值设定项的定义放在完全一个源文件中。
与函数类似,将函数的声明放在 header 中,将函数的定义放在一个源文件中。
我正在自学 C。我按照教程进行操作,得到了一个可以在屏幕上四处移动的图像。现在我正在尝试自己做并了解如何模块化我的代码并知道它发生了什么。
我构建了一个结构来获取玩家坐标并将其调用到我的 game_loop.h 文件中。但它不允许我从结构中设置变量。我试图只包括重要的部分以保持简洁。让我知道是否需要 post 整个代码。 我究竟做错了什么。
//includes
#include "game_loop.h"
//main body
int main( int argc, char *argv[])
{
//TODO make game menu and link it here
//TODO make game loop and put it here
initSDL();
renderGame();
handleEvent();
//make game cleanup and put it her
destroySDL();
return 0;
}
int头文件game_loop.h -->
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "player.h"
#define pSIZE 64
#define wWIDTH 1280
#define wHEIGHT 720
//variables for starting SDL
SDL_Event event;
SDL_Window *window = NULL;
SDL_Renderer *render = NULL;
SDL_Surface *bgSurface = NULL;
SDL_Texture *bgTexture = NULL;
SDL_Surface *pSurface = NULL;
SDL_Texture *pTexture = NULL;
int flags = 0; //window flags may need to change in the future
struct Player player;
player.x = 600;
player.y = 300;
void initSDL()
{
//initializing SDL
if(SDL_Init(SDL_INIT_VIDEO)!= 0)
{
printf("ERROR starting SDL: %s\n", SDL_GetError());
}else{printf("Starting SDL: Successful.\n");}
在 player.h 文件中 -->
struct Player{
int x;
int y;
};
您在函数之外有几行可执行代码:
Player player;
player.x = 600;
player.y = 300;
第一行定义了一个变量。那没问题。接下来的两行不是,因为它们是语句。
您需要在定义结构时对其进行初始化。您可以按如下方式进行:
Player player = { 600, 300 };
此外,在 header 文件中定义变量也不是一个好主意。如果在多个源文件中使用 header,您最终会因多个定义而出错。
在您的 header 文件中,变量应声明为 extern
而无需初始化程序:
extern Player player;
然后你会把带有初始值设定项的定义放在完全一个源文件中。
与函数类似,将函数的声明放在 header 中,将函数的定义放在一个源文件中。