gpt4 book ai didi

c++ - Typedef 指向 C++ 中同名指针的结构

转载 作者:太空狗 更新时间:2023-10-29 23:43:18 25 4
gpt4 key购买 nike

我试图在我的代码中包含一个由某些编译器自动生成的 (.h) 头文件。以下是自动生成的头文件中的代码片段。

typedef struct SequenceOfUint8 {        // Line 69
struct SequenceOfUint8 *next;
Uint8 value;
} *SequenceOfUint8; // Line 72

如果我在 C 代码(gcc 编译器)中包含这个头文件,它可以正常编译,没有任何错误,但如果尝试在 CPP 代码中包含它,g++ 编译器会抛出下面提到的错误。

In file included from ssme/src/../include/xxxxx.h:39:0,
from ssme/src/ssme.cpp:11:
ssme/src/../include/yyyyy.h:72:4: error: conflicting declaration ‘typedef struct SequenceOfUint8* SequenceOfUint8’
} *SequenceOfUint8;
^~~~~~~~~~~~~~~
ssme/src/../include/yyyyy.h:69:16: note: previous declaration as ‘struct SequenceOfUint8’
typedef struct SequenceOfUint8 {
^~~~~~~~~~~~~~~

谁能告诉我如何在 C++ 代码中使用它(如果可能的话,不要更改自动生成的代码)。

PS:我使用 extern "C"{ #include "yyyy.h"} 语句将头文件包含在 CPP 文件中,仍然没有成功。

最佳答案

您不能像在 C++ 代码中那样使用它。这是 C 和 C++ 作为两种不同语言的另一个例子。

C 中的标记命名空间是独立的,而在 C++ 中则不是。准确地说,它甚至不存在于 C++ 中。

extern "C" 中包装也不会使 C++ 编译器将 header 视为 C 代码。这不是预期的功能。 header 必须是独立有效的 C++,而事实并非如此。

您需要编写一个 C 包装器,它公开一个与 C++ 兼容的 API。

关于c++ - Typedef 指向 C++ 中同名指针的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54827163/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com