我的类中几乎所有的构造函数和解构函数都被隐式定义为已删除?

Almost all of the constructors and deconstructors in my classes are implicitly defined as deleted?

本文关键字:定义 删除 函数 和解 构造函数 我的      更新时间:2023-10-16

Variable.h

#pragma once
#include <unordered_map>
namespace ssu
{
class GenericArray;
class Variable
{
enum Type
{
NONE,
BOOL,
STRING,
INT,
FLOAT,
ARRAY,
DICT,
TYPE_MAX
};
private:
template<typename Key, typename Value>
using Dictionary = std::unordered_map<Key, Value>;
union
{
bool _bool;
std::string _string;
int64_t _int;
double _float;
GenericArray _array;
Dictionary<std::string, Variable&> _dict;
};
Type m_Type;
private:
static inline Variable eval();
public:
Variable(const bool p_bool);
Variable(const std::string p_string);
Variable(const int64_t p_int);
Variable(const double p_float);
Variable(const GenericArray p_array);
Variable(const Dictionary<std::string, Variable&> p_dict);
};
class GenericArray
{
private:
struct Block
{
Variable m_Data;
Block* m_Next;
Block* m_Prev;
};
Block* m_First;
Block* m_Last;
size_t m_Size = 0;
public:
void push_back(Variable p_var);
void erase(int index);
void erase(int first, int end);
Variable& operator[] (int index);
};
}

可变.cpp

#include "variable.h"
namespace ssu
{
void GenericArray::push_back(Variable p_var)
{
Block* block = new Block;
block->m_Data = p_var;
if (!m_First || !m_Last)
{
m_First = block;
m_Last = block;
}
}
}

我对我遇到的错误感到非常困惑。我尝试编译代码,但它不会构建。这在以前从未发生过。

如果很重要,Variable类旨在用作联合中所有值的泛型类型。也许GenericArrayVariable之间的递归导致了这个问题?

这些是我遇到的错误类型:

File    Severity    Code    Line    Description
variable.cpp    Error   C2280   6   'ssu::Variable::~Variable(void)': attempting to reference a deleted function
variable.cpp    Error   E1790   7   the default constructor of "ssu::GenericArray::Block" cannot be referenced -- it is a deleted function
variable.cpp    Error   E1776   8   function "ssu::Variable::operator=(const ssu::Variable &)" (declared implicitly) cannot be referenced -- it is a deleted function
variable.h      Error   C2079   36  'ssu::Variable::_array' uses undefined class 'ssu::GenericArray'
variable.h      Warning C4624   50  'ssu::Variable': destructor was implicitly defined as deleted
variable.h      Warning C4624   60  'ssu::GenericArray::Block': destructor was implicitly defined as deleted
variable.h      Error   C2079   36  'ssu::Variable::_array' uses undefined class 'ssu::GenericArray'
variable.h      Warning C4624   50  'ssu::Variable': destructor was implicitly defined as deleted
variable.h      Warning C4624   60  'ssu::GenericArray::Block': destructor was implicitly defined as deleted
variable.cpp    Error   C2280   7   'ssu::GenericArray::Block::Block(void)': attempting to reference a deleted function
variable.cpp    Error   C2280   8   'ssu::Variable &ssu::Variable::operator =(const ssu::Variable &)': attempting to reference a deleted function

联合声明

联合不能包含具有非平凡特殊成员函数(复制构造函数、复制赋值运算符或析构函数(的非静态数据成员。

如果联合包含具有非平凡特殊成员函数(复制/移动构造函数、复制/移动赋值或析构函数(的非静态数据成员,则该函数在联合中默认删除,并且需要由程序员显式定义。

如果联合包含具有非平凡默认构造函数的非静态数据成员,则默认情况下将删除联合的默认构造函数,除非联合的变体成员具有默认成员初始值设定项。

具有非平凡默认构造函数的非静态数据成员是 std::string 和 Dictionary。

如果类数据成员具有已删除的构造函数,则默认情况下将删除类默认构造函数。