几个月来,我一直试图自己解决这个问题,这并不夸张。
使用我不使用的方法时,我感到很脏,因为当事情变得复杂时,事情往往会破裂并超出范围,而我在任何地方都找不到答案。
我有一个项目结构如下:
Project-Directory/
main.cpp
makefile
SDLMain.h // SDL libraries required to be in the project directory.
SDLMain.m // SDL libraries required to be in the project directory.
--- gamestate/
------ clean.cpp
------ gamestate.cpp
------ gamestate.h
------ init.cpp
--- graphics/
------ graphics.h
------ // more .cpp files
--- logic/
------- logic.h
------- // more .cpp files
--- character
------- character.h
------- // more .cpp files
在main.cpp
我有:
// C++ Libraries
#include <iostream>
// Graphical Libraries
//#include <SDL/SDL.h>
//#include <SDL/SDL_opengl.h>
// Custom Libraries
#include "character/character.h"
#include "logic/logic.h"
#include "graphics/graphics.h"
#include "gamestate/gamestate.h"
字符和图形等中的所有 .cpp 文件都包含它们各自与文件夹同名的头文件。即clean.cpp
和gamestate.cpp
所有init.cpp
包括gamestate.h
。
每个文件夹中只有一个头文件,最近从每个 .cpp 1 个头文件重组而来。
基本上,在这个新的、更结构化的系统上,当我尝试编译我的项目时,我会遇到范围错误。
如果我的头文件被包含在之后#include <iostream>
并且 SDL 库包含在main.cpp
.
我通过将其插入所有头文件来解决了该错误:
// C++ Libraries
#include <iostream>
// Graphical Libraries
#include <SDL/SDL.h>
#include <SDL/SDL_opengl.h>
但后来我一遍又一遍地包含同样的东西,这肯定是不好的做法。
不仅如此,还gamestate.h
包含一个函数使用的原型,gamestate.cpp
在该函数中logic.cpp
会出现范围错误,除非我隐式包含logic.h
在其中,gamestate.h
即使logic.h
它包含在main.cpp
before中gamestate.h
。
我认为#include
这是为了将头文件的内容拖到范围和它的原型中,以便编译器知道会发生什么。
为什么我会收到所有这些关于范围和功能不存在的错误?
我应该在那里制作一个global.h
和#include
所有的 SDL 和<iostream>
东西吗?
为什么我不能logic.h
从后面包含的另一个文件访问原型化的函数main.cpp
?