Here is an oversimplified version of my code
#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;
}
basically there is a node which has a type and a bunch of children. the type is basically a tagged union, I make sure that if i am reading an element of the union it is the one I last wrote. This seems simple enough to me, so I am not sure why I am getting the errors.
here are all the errors
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()’
Why is ~NodeType() deleted? How can i fix this? any help would be appreciated.
CodePudding user response:
You have a union with a member that has a non-trivial destructor. So the union can't know how to destruct itself. That's why NodeType::~NodeType
is deleted by default. That's what the message tells you.
You need to define the destructor yourself and properly call the destructor on the correct member.
Or, better yet, don't use unions at all. They are unsafe. Use std::variant
which is the standard library type for "tagged unions", or type-safe unions.