Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/64.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
C 如何解决函数采用结构的循环依赖性问题?_C_Emulation_Sdl 2 - Fatal编程技术网

C 如何解决函数采用结构的循环依赖性问题?

C 如何解决函数采用结构的循环依赖性问题?,c,emulation,sdl-2,C,Emulation,Sdl 2,我正试图解决一个问题,即一个标头中的函数需要一个结构作为参数,该结构包含与函数相同的标头中的结构。代码如下: 任天堂 寄存器.h 如您所见,“InitializeRegisters”函数将任天堂结构作为参数,但任天堂结构在其定义中包含寄存器结构。这会导致循环依赖性问题 我知道我可以通过让参数接受Registers*并通过&Nintendo.reg来解决这个问题,但我不想这样做 以下是错误输出: In file included from source/Nintendo.h:13: source/

我正试图解决一个问题,即一个标头中的函数需要一个结构作为参数,该结构包含与函数相同的标头中的结构。代码如下:

任天堂 寄存器.h 如您所见,“InitializeRegisters”函数将任天堂结构作为参数,但任天堂结构在其定义中包含寄存器结构。这会导致循环依赖性问题

我知道我可以通过让参数接受Registers*并通过&Nintendo.reg来解决这个问题,但我不想这样做

以下是错误输出:

In file included from source/Nintendo.h:13:
source/Registers.h:31:26: error: unknown type name 'Nintendo'
void InitializeRegisters(Nintendo *nes);
                         ^
1 error generated.
In file included from source/Registers.h:5:
source/Nintendo.h:17:5: error: unknown type name 'Registers'
    Registers reg;
    ^
1 error generated.
/bin/ld: /bin/../lib64/gcc/x86_64-pc-linux-gnu/8.2.0/../../../../lib64/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:2: test] Error 1

最简单的答案是添加一行:

typedef struct Nintendo Nintendo;
靠近registers.h的顶部(对头文件使用混合大小写是一个非常糟糕的主意)。C语言的一个特点是,typedef语句可以重复,只要它们都是一致的,并且充当“转发声明”,这正是您所需要的


如果你想变得乖张,你可以把它放在nintendo.h的ifndef行上方,但是如果这是你的事情,还有其他方法可以让你开心。

为什么名为
InitializerRegisters
的函数会采用
nintendo
结构?为什么不像名字所暗示的那样,一个
寄存器*
?你必须先定义寄存器,然后是任天堂,然后是初始化寄存器;都是非常专业的,非常感谢。只是想知道,为什么头的混合情况是不好的?这是一个不好的建议,围绕一个糟糕的程序架构工作。@ConnerTurmon:不是,这只是一个单一的意见。实际上,如果使用CamelCase或mixedCase,这是非常常见的。另请参阅我上面的评论。头文件基本上是一个外部或系统提供的构造。DOS和Windows做出了愚蠢得惊人的决定,忽略了文件系统中的案例;马科斯紧随其后。一旦他们发现了错误,他们就做出了更糟糕的决定,因此有时x.h匹配x.h,有时则不匹配。最终的结果是,不要使用混合案例,因为您不能依赖于您所依赖的操作系统的制定者的决策。他们就是没那么聪明。
In file included from source/Nintendo.h:13:
source/Registers.h:31:26: error: unknown type name 'Nintendo'
void InitializeRegisters(Nintendo *nes);
                         ^
1 error generated.
In file included from source/Registers.h:5:
source/Nintendo.h:17:5: error: unknown type name 'Registers'
    Registers reg;
    ^
1 error generated.
/bin/ld: /bin/../lib64/gcc/x86_64-pc-linux-gnu/8.2.0/../../../../lib64/Scrt1.o: in function `_start':
(.text+0x24): undefined reference to `main'
clang-6.0: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:2: test] Error 1
typedef struct Nintendo Nintendo;