我试图组织我的项目,所以我想我会包括我的全局变量,#includes和结构定义在global.h头文件中。然而,我无法完全理解这个概念,而构建过程中的错误似乎证明了这一点。当我尝试在logic.h中访问我的global.h时,会发生什么情况。在单独的头文件中的C++结构
global.h:
#ifndef GLOBAL_H
#define GLOBAL_H
#include "logic.h"
#include <SDL.h>
#include <iostream>
#include <string>
#include "graphics.h"
//Global variables and structs
enum directions
{
D_UP,
D_LEFT,
D_DOWN,
D_RIGHT,
D_TOTAL
};
struct Character
{
float health;
float x;
float y;
float velocity;
bool collision[D_TOTAL];
directions direction;
SDL_Rect animation;
SDL_Rect sprite;
};
const int windowWidth = 800;
const int windowHeight = 600;
const int frameWidth = 64;
const int frameHeight = 64;
#endif // GLOBAL_H
logic.h:
#include "global.h"
//Header for gamelogic functions
//Initialization of all variables for a new character
void initCharacter(Character &newCharacter, float x, float y, directions startingDirection);
当我尝试构建它,这是我得到的错误:
||=== Build: Debug in GameProject0.2 (compiler: GNU GCC Compiler) ===|
C:\Users\Rafał\Documents\GameProject0.2\GameProject0.2\logic.h|5|error: variable or field 'initCharacter' declared void|
C:\Users\Rafał\Documents\GameProject0.2\GameProject0.2\logic.h|5|error: 'Character' was not declared in this scope|
C:\Users\Rafał\Documents\GameProject0.2\GameProject0.2\logic.h|5|error: 'newCharacter' was not declared in this scope|
C:\Users\Rafał\Documents\GameProject0.2\GameProject0.2\logic.h|5|error: expected primary-expression before 'float'|
C:\Users\Rafał\Documents\GameProject0.2\GameProject0.2\logic.h|5|error: expected primary-expression before 'float'|
C:\Users\Rafał\Documents\GameProject0.2\GameProject0.2\logic.h|5|error: 'directions' was not declared in this scope|
||=== Build failed: 6 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
我不知道我错过了什么。感谢您的任何建议!
谢谢,但我认为它清楚地说#include“global.h”在logic.h文件中。我认为这会完成这项工作,但不知何故:( –
@RafałMyśliwczyk你还在global.h文件中包含了logic.h。当你包含包含global.h的logic.h时,你会怎么想呢?包括logic.h,其中包含global.h,其中包括logic.h,其中包含global.h,其中包括logic.h,其中包含global.h,其中包含logic.h,其中包含global.h,其中包含logic.h,其中包含global.h包括logic.h,其中包含global.h,其中包含logic.h,其中包含global.h,其中包含logic.h,其中包含global.h,其中包含logic.h,其中包含global.h,其中包含logic.h ...? – user2079303
A关于避免循环依赖和使用[包括守卫](https://en.wikipedia.org/wiki/Include_guard)的良好警示性的故事。两个为一个的价格。 – user4581301