修复循环依赖项 c++17 标头
Fixing circular dependencies c++17 headers
我正在使用 clang C++17 编译器,并收到警告:
declaration of 'struct Xchart' will not be visible outside of this function.
此警告指向使用在不同头文件中声明的结构的函数声明。我相信这是由两个头文件中的循环依赖关系引起的,但我无法解决警告
Header toolkit.h 声明了函数 MyFunction,它使用结构 Xchart 作为输入。这是警告所指出的地方。
工具包.h
#ifndef _TOOLKIT_H
#define _TOOLKIT_H 1
#define _WINDOWS 1
#include <windows.h>
short WINAPI MyFunction(struct Xchart *mychart ); <--Warning Here
#pragma pack(push, 1)
#pragma pack(pop)
#endif /*_TOOLTKIT_H */
标头 mystruct.h 声明了 Xchart 结构
mystruct.h
#ifndef _mystructs_h
#define _mystructs_h 1
#include "toolkit.h"
#pragma pack(push, 1)
struct Xchart {
int MyDays;
short LoadMe;
wchar_t MyLabel[100];
};
#pragma pack(pop)
#endif /* _mystructs_h */
您能否展示如何更改这两个头文件以解决警告?
通常的解决方法很简单:
struct Xchart; // declares Xchart; definition is elsewhere.
short WINAPI MyFunction(Xchart *mychart); // Function declaration.
只有工具包.cpp需要定义Xchart
,但.cpp文件本身不包括在其他地方,并且不会对循环包含做出贡献。
有两个不同的问题。
1(在下面struct
关键字在这里是有问题的:
short WINAPI MyFunction(struct Xchart *mychart )
它应该如下所示Xchart
在此之前应该声明:
short WINAPI MyFunction(Xchart *mychart )
从声明中删除关键字。
2(您必须反转标题包含。mystruct.h 应包含在 toolkit.h 中。因为你想使用mystruct.h中定义的结构。从我的严格.h中删除工具包.h。
相关文章:
- g++ 说函数不存在,即使包含正确的标头
- spdlog标头仅与外部fmt一起使用.spdlog错误:'内部':不是'fmt'
- 在CMake中使用find_package时,是否会显式包含标头
- 错误"Could not find Boost"(缺少:上下文标头)
- 如何在标头中声明(或定义)函数的问题
- 如何避免在仅标头库中C++类/变量重定义
- C++算法标头中,为什么要使用 "!(val < *first)" ?
- C++标头错误 C2238 意外标记";"
- std::initializer_list,大括号初始化和标头
- 在 c++ 中拆分类和标头中的继承,错误
- 如何确定哪个标头调用 c++ 中的另一个标头
- 如何在生成文件中添加多个标头 (HDR) 和对象?
- 我可以在运行时重新定义在 OpenCascade/OCCT 标头中定义的 c++ 静态常量吗?
- GCC,CMake,预编译标头和维护依赖项
- VisualStudio:使用 Suse Enterprise Server 12 SP5 时,不会下载远程库标头
- 一个标头库中的错误
- 可视化 Bazel C++预编译标头实现
- 如何在 CMake 中添加预编译标头用于 Visual Studio 生成器
- 修复循环依赖项 c++17 标头
- 为什么 Xcode 11 beta 不能使用 C++17 的<filesystem>标头?