单独 header 文件中的 C++ 结构
C++ struct in separate header file
我试图组织我的项目,所以我认为我将在 global.h header 中包含我所有的全局变量、#includes 和结构定义。但是我不能完全理解这个概念,构建过程中的错误似乎证明了这一点。当我尝试在 logic.h 中访问我的 global.h 时,会发生这种情况。
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"
时,第一个 它说的事情之一是 include "logic.h"
,因此处理器打开该文件,找到一个 include "global.h"
,由于包含防护,它什么都不做,然后它找到 void initCharacter(Character &newCharacter,
并感到困惑,因为它还不知道 Character
是什么。然后它returns从那个包含,词法enum directions
,然后最后词法struct Character
,所以然后它知道什么是 Character
,为时已晚。
一般来说,尽量确保只包含一个方向。确保 logic
包含 global
,或者 global
包含 logic
,但不能同时包含两者。
或者,如果您真的很懒惰,有时可以使用一个技巧来回避这个问题:将函数和 class 定义尽可能多地放在包含之前。然后 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
{
...
我试图组织我的项目,所以我认为我将在 global.h header 中包含我所有的全局变量、#includes 和结构定义。但是我不能完全理解这个概念,构建过程中的错误似乎证明了这一点。当我尝试在 logic.h 中访问我的 global.h 时,会发生这种情况。
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"
时,第一个 它说的事情之一是 include "logic.h"
,因此处理器打开该文件,找到一个 include "global.h"
,由于包含防护,它什么都不做,然后它找到 void initCharacter(Character &newCharacter,
并感到困惑,因为它还不知道 Character
是什么。然后它returns从那个包含,词法enum directions
,然后最后词法struct Character
,所以然后它知道什么是 Character
,为时已晚。
一般来说,尽量确保只包含一个方向。确保 logic
包含 global
,或者 global
包含 logic
,但不能同时包含两者。
或者,如果您真的很懒惰,有时可以使用一个技巧来回避这个问题:将函数和 class 定义尽可能多地放在包含之前。然后 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
{
...