C ++ 17可选树,错误:无效使用不完整类型 [英] C++17 optional tree, error: invalid use of incomplete type

查看:94
本文介绍了C ++ 17可选树,错误:无效使用不完整类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我编译包含可选类型的二叉树时:

When I compile a binary tree containing optional types:

#include <optional>

class BinaryTree
{
public:
    BinaryTree();
    int value;
    std::optional<BinaryTree> left,right;
};

int main()
{
    return 0;
}

通过

g++ -std=c++17 -Wfatal-errors main.cpp 

我遇到这个错误

In file included from /usr/include/c++/7/bits/move.h:54:0,
                 from /usr/include/c++/7/bits/stl_pair.h:59,
                 from /usr/include/c++/7/utility:70,
                 from /usr/include/c++/7/optional:36,
                 from main.cpp:1:
/usr/include/c++/7/type_traits: In instantiation of ‘struct std::is_trivially_copy_constructible<BinaryTree>’:
/usr/include/c++/7/optional:103:8:   required from ‘class std::_Optional_base<BinaryTree>’
/usr/include/c++/7/optional:451:11:   required from ‘class std::optional<BinaryTree>’
main.cpp:8:28:   required from here
/usr/include/c++/7/type_traits:1409:12: error: invalid use of incomplete type ‘class BinaryTree’
     struct is_trivially_copy_constructible
            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated due to -Wfatal-errors.

我应该如何解决此错误?

How should I fix this error?

推荐答案

可选,在使用时包含该类型的完整实例.

Optional contains the complete instance of the type when it is engaged.

在一个类型的实例中存储一个类型的两个实例是不可能的;它需要2< = 1.

Storing two instances of a type within an instance of the type is impossible; it requires 2<=1.

考虑使用指向该类型的唯一指针而不是可选指针.

Consider using a unique pointer to the type instead of an optional.

如果您希望能够复制树,则必须编写一个值指针.值指针是知道如何进行深层复制的指针.

If you want to be able to copy the tree then you have to write a value pointer. Value pointer is a pointer that knows how to deep copy itself.

这篇关于C ++ 17可选树,错误:无效使用不完整类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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