Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/128.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
对头文件的混淆 我是一个相当新的程序员,我在VisualStudio/C++ +SDL中做了一个小游戏项目。我的代码如下所示:_C++_Visual Studio 2012_Sdl_Header Files - Fatal编程技术网

对头文件的混淆 我是一个相当新的程序员,我在VisualStudio/C++ +SDL中做了一个小游戏项目。我的代码如下所示:

对头文件的混淆 我是一个相当新的程序员,我在VisualStudio/C++ +SDL中做了一个小游戏项目。我的代码如下所示:,c++,visual-studio-2012,sdl,header-files,C++,Visual Studio 2012,Sdl,Header Files,A.h: #ifndef PROTOTYPES_H #define PROTOTYPES_H #include "constants.h" #include "globals.h" #include "functions.h" struct circle { float x; float y; int r; }; //other class declarations #endif PROTOTYPES_H 职能.h: #ifndef FUNCTIONS_H #def

A.h:

#ifndef PROTOTYPES_H
#define PROTOTYPES_H

#include "constants.h"
#include "globals.h"
#include "functions.h"

struct circle
{
    float x;
    float y;
    int r;
};
//other class declarations
#endif PROTOTYPES_H
职能.h:

#ifndef FUNCTIONS_H
#define FUNCTIONS_H

#include "SDL_header.h"
#include "prototypes.h"

bool check_collision(circle circle, SDL_Rect rect);
//other function declarations

#endif FUNCTIONS_H
据我所知,至少根据解释,这是完全正确的。 即使我将鼠标移到IDE中functions.h文件中的“圆圈”上,也会弹出正确的工具提示“struct circle”。然而,当我编译时,我在引用函数时得到了错误“未声明的标识符”圆圈“


怎么回事?

您当前有一个循环包含。Prototype.h包括functions.h和functions.h包括Prototype.h

现在,如果您正在编译的cpp文件首先调用prototype.h,那么第二行将包含functions.h的内容,它将在实际定义之前放置对circle struct的引用

在您的情况下,如果functions.h包含prototype.h中定义的结构上的函数,则没有任何理由包含prototype.h中的functions.h。 如果管理头文件之间的依赖关系,则代码将更易于管理

就你而言:

  • function.h取决于prototype.h,并且必须包含它
  • prototype.h不能包含function.h
此外,将类/结构及其所有函数保存在单个头文件中通常是值得的。除非文件变得太大,在这种情况下,您可以将其拆分为多个标题


注意:蛮力解决方案可以是在函数中使用结构圆之前添加一个到结构圆。但是,我只会在无法避免的情况下保留此类解决方案,谢谢。现在我想这是很明显的。那么,如果我需要我的类能够访问我的函数,我的函数能够访问我的类,那么构建我的程序的更好方法是什么呢?为什么你需要prototype.h来了解function.h?原型中没有任何东西。h取决于函数。所以您不需要包含。prototype.h包含3个文件,但没有使用其中任何一个文件中的任何内容。prototype.h中还有其他依赖于函数的类,我缩短了该文件以便于发布。事实上,虽然我认为我可以将它包含在每个类中。cpp是为了让它正常工作,但最初我只是在prototype.h中包含了函数.h,这样我的所有类都可以在需要时访问函数。使用一次
#pragma的奢侈功能