c++ - 结构中类型不完整的 c++ unique_ptr 即使使用析构函数也无法编译
问题描述
我正在处理一个大型代码库。我需要将结构作为唯一指针成员变量公开给头文件。这个结构存在于一个单独的头文件中,我不想在原始头文件中包含这个结构,因为它可能会经常更新,这将导致使用原始头文件的多个二进制文件大小不匹配,除非它们都被重新编译。我现在要做的是在原始头文件中前向声明结构并在唯一指针中使用不完整的类型。
以下是文件的基本结构:
Original header (info.h) - This header file is added to multiple cpp files in this particular directory:
#include <memory>
struct options_t;
typedef struct info
{
int a;
int b;
string c;
info() : a(0), b(0), c("") {}
~info();
unique_ptr<options_t> m_option;
} info_t;
New header (options.h) - This header is only added to cpp files that need info options
typedef struct options
{
enum bits
{
option_a = 1,
option_b = 2
}
unsigned long long flag = 0ULL;
} options_t;
这是我在尝试编译此代码时遇到的错误:
error: invalid application of 'sizeof' to incomplete type 'options_t'
79 | static_assert(sizeof(_Tp)>0,
| ^~~~~~~~~~~
我也尝试添加info_t::~info() = default;
到所有包含原始信息头的 cpp 文件,但我仍然得到同样的错误。有人可以告诉我我做错了什么吗?