error: use of deleted function ‘Node::~Node()’

error: use of deleted function ‘Node::~Node()’

这是我的代码的简化版本

    #include <vector>
    #include <string>
    #include <unordered_map>
    #include <iostream>




    struct ElementData
    {
        std::string tagName;
        std::unordered_map<std::string,std::string> attributes;
    };

    enum NodeTypeEnum
    {
        None=-1,
        Text,
        Element
    };


    struct NodeType
    {
    private:

        union
        {
            std::string text;
            ElementData element;
        };
        
    public:
        NodeTypeEnum type;


    };



    struct Node
    {
        std::vector<Node> children;    
        NodeType type;


    };




    int main()
    {
        
        Node n;
        
    }

基本上有一个节点有一个类型和一堆children。 该类型基本上是一个标记的联合,我确保如果我正在阅读联合的一个元素,它就是我上次写的那个。这对我来说似乎很简单,所以我不确定为什么会收到错误。

这里是所有的错误

Main.cpp: In function ‘int main()’:
Main.cpp:55:10: error: use of deleted function ‘Node::Node()’
   55 |     Node n;
      |          ^
Main.cpp:41:8: note: ‘Node::Node()’ is implicitly deleted because the default definition would be ill-formed:
   41 | struct Node
      |        ^~~~
Main.cpp:41:8: error: use of deleted function ‘NodeType::NodeType()’
Main.cpp:23:8: note: ‘NodeType::NodeType()’ is implicitly deleted because the default definition would be ill-formed:
   23 | struct NodeType
      |        ^~~~~~~~
Main.cpp:29:21: error: union member ‘NodeType::<unnamed union>::text’ with non-trivial ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
   29 |         std::string text;
      |                     ^~~~
Main.cpp:30:21: error: union member ‘NodeType::<unnamed union>::element’ with non-trivial ‘ElementData::ElementData()’
   30 |         ElementData element;
      |                     ^~~~~~~
Main.cpp:41:8: error: use of deleted function ‘NodeType::~NodeType()’
   41 | struct Node
      |        ^~~~
Main.cpp:23:8: note: ‘NodeType::~NodeType()’ is implicitly deleted because the default definition would be ill-formed:
   23 | struct NodeType
      |        ^~~~~~~~
Main.cpp:29:21: error: union member ‘NodeType::<unnamed union>::text’ with non-trivial ‘std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::~basic_string() [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]’
   29 |         std::string text;
      |                     ^~~~
Main.cpp:30:21: error: union member ‘NodeType::<unnamed union>::element’ with non-trivial ‘ElementData::~ElementData()’
   30 |         ElementData element;
      |                     ^~~~~~~
Main.cpp:55:10: error: use of deleted function ‘Node::~Node()’
   55 |     Node n;
      |          ^
Main.cpp:41:8: note: ‘Node::~Node()’ is implicitly deleted because the default definition would be ill-formed:
   41 | struct Node
      |        ^~~~
Main.cpp:41:8: error: use of deleted function ‘NodeType::~NodeType()’

为什么 ~NodeType() 被删除了?我怎样才能解决这个问题? 任何帮助将不胜感激。

您有一个联合,其成员具有非平凡的析构函数。所以工会不知道如何自我毁灭。这就是默认删除 NodeType::~NodeType 的原因。这就是消息告诉你的内容。

您需要自己定义析构函数并在正确的成员上正确调用析构函数。

或者,更好的是,根本不使用联合。他们不安全。使用 std::variant,这是“标记联合”或类型安全联合的标准库类型。