“使用未定义类型"与unique_ptr一起转发声明的类和默认的移动构造函数/赋值 [英] "Use of undefined type" with unique_ptr to forward declared class and defaulted move constructor/assignment

查看:44
本文介绍了“使用未定义类型"与unique_ptr一起转发声明的类和默认的移动构造函数/赋值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,是避免编译错误并包括B.h在A.cpp中手动实现move构造函数/赋值的唯一方法吗?

In the following code, is the only way to avoid a compile error and to include B.h implementing the move constructor / assignment manually in 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给出了错误C2027:使用未定义类型",因为std :: unique_ptr的move构造函数/赋值运算符在m_b上调用了Deleter(试图调用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.

推荐答案

是的,您需要从实例化 std :: unique_ptr< B>的任何地方访问 B 的完整定义.; :: ~~ unique_ptr ,因为它需要调用 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.

在您的情况下,这意味着必须将 A ::〜A 的定义移到单独的 A.cpp 文件中,该文件应包含 Bh .

In your case, that means that A::~A's definition must be moved to a separate A.cpp file, which includes B.h.

这篇关于“使用未定义类型"与unique_ptr一起转发声明的类和默认的移动构造函数/赋值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆