2017-09-13 45 views
-1

我试图组织我的项目,所以我想我会包括我的全局变量,#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)) ===| 

我不知道我错过了什么。感谢您的任何建议!

回答

0

当你include "global.h",的第一东西它说一个是include "logic.h",因此处理器打开这个文件,找到一个include "global.h",这确实是因为包括后卫的什么都没有,然后发现void initCharacter(Character &newCharacter,和迷糊,因为它不还不知道Character是什么。然后它从那个包含中返回,然后是enum directions,然后最后是从而struct Character,所以然后它知道什么是Character,太迟了。

一般来说,尽量确保只包含一种方法。确保logic包含global,或者global包含logic,但不是两者都包含。或者,如果你真的懒惰,有时候会回避这个问题:把函数和类定义放在包含之前,尽可能频繁。然后global会告诉编译器,class Character存在,那么当它包含logic.h时,它不会有问题。

#ifndef GLOBAL_H 
#define GLOBAL_H 

enum directions; 
struct Character; //If you're really lazy, this usually works 

#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 
{ 
    ... 
+0

谢谢,但我认为它清楚地说#include“global.h”在logic.h文件中。我认为这会完成这项工作,但不知何故:( –

+0

@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

+0

A关于避免循环依赖和使用[包括守卫](https://en.wikipedia.org/wiki/Include_guard)的良好警示性的故事。两个为一个的价格。 – user4581301