[英]"Use of undefined type" with unique_ptr to forward declared class and defaulted move constructor/assignment
In the following code, is the only way to avoid a compile error and to include Bh implementing the move constructor / assignment manually in A.cpp?在下面的代码中,是避免编译错误并包含 Bh 在 A.cpp 中手动实现移动构造函数/赋值的唯一方法吗?
// A.h
#include <memory>
class B; // implementation somewhere in B.h/B.cpp
class A
{
public:
A() = default;
~A() = default;
A(const A&) = delete;
A& operator=(const A&) = delete;
A(A&&) = default;
A& operator=(A&&) = default;
private:
std::unique_ptr<B> m_b;
};
Visual Studio 2015 gives "error C2027: use of undefined type", since the move constructor/assignment operator of std::unique_ptr calls the deleter on m_b (trying to invoke B's destructor), which is obviously not known at this point. Visual Studio 2015 给出“错误 C2027:使用未定义类型”,因为 std::unique_ptr 的移动构造函数/赋值运算符调用 m_b 上的删除器(试图调用 B 的析构函数),这在这一点上显然是未知的。
Yes, you need to have access to the full definition of B
from wherever you instantiate std::unique_ptr<B>::~unique_ptr
, because it needs to call B
's destructor.是的,您需要从任何实例化
std::unique_ptr<B>::~unique_ptr
地方访问B
的完整定义,因为它需要调用B
的析构函数。
In your case, that means that A::~A
's definition must be moved to a separate A.cpp
file, which includes Bh
.在您的情况下,这意味着
A::~A
的定义必须移动到单独的A.cpp
文件,其中包括Bh
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.