我最近遇到了一些违背我对包含的理解的东西。
我正在创建一个 dll 来保存我正在开发的个人游戏引擎的基本坐标对象(为了好玩)。
主要的 dll 头文件。
#pragma once
#include "sclapi.h"
#include "vector.h"
#include "point.h"
// Other includes and stuff.
// Unimportant for this demonstration.
sclapi.h
#pragma once
#ifdef SCL_EXPORTS
#define SCL_API __declspec(dllexport)
#else
#define SCL_API __declspec(dllimport)
#endif
vector .h
#pragma once
// No includes
/*EDIT*/struct Point;
struct SCL_API Vector {
float x, y;
// Other stuff
explicit operator Point() const;
};
点.h
#pragma once
// No includes
struct SCL_API Point {
int x, y;
// Other stuff
explicit operator Vector() const;
};
我的代码工作得很好;但据我了解,它不应该。头文件应该只知道其中声明的内容(包括粘贴代码的简写)。这些对象都没有在对方的头文件中声明。 [已编辑] point.h 应该不知道 Vector 结构。更重要的是,他们甚至都知道 SCL_API 宏。如果我注释掉主头文件中的个别包含,我会得到预期的编译器错误。我错过了什么?
编辑:
进一步测试后,我发现后面的对象的声明需要在第一个头文件'vector.h'中;但之后,它们不需要在任何其他头文件中再次声明。此外,在主头文件中声明类也不起作用。 Point forward 声明必须在 vector.h 文件中。
最佳答案
当您#include
一个文件时,它会自动“复制粘贴”到您的源代码中。
因为您在包含 point.h
之前包含了 vector.h
,所以 Point
类将看到它。
但是,依赖这种行为并不是一个好主意,因为包含的顺序可能会改变,因此它不再起作用,所以你应该在你的 点中
.#include "vector.h"
.h
关于c++ - 对包含在 DLL 中的说明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50494052/