简体   繁体   English

错误C2280:在声明C ++结构时尝试引用已删除的函数

[英]error C2280: attempting to reference a deleted function while declaring a C++ struct

I have the following C++ code (in VS2017): 我有以下C ++代码(在VS2017中):

struct OptionA
{
    std::string s1;
};

struct OptionB
{
    std::string s2;
};

struct Inner
{
    int b;
    union {
        OptionA optA;
        OptionB optB;
    }Options;

};

struct Outer
{
    int a;
    Inner b;
}

When I tried to declare this struct: 当我试图声明这个结构时:

int main()
{
   Outer obj; 
};

I get the compilation error: 我收到编译错误:

error C2280: 'Outer::Outer(void)': attempting to reference a deleted function 错误C2280:'Outer :: Outer(void)':尝试引用已删除的函数

This way of declaring a struct should work fine. 这种声明结构的方式应该可以正常工作。 I feel this error is something to do with the constructor of the structure. 我觉得这个错误与结构的构造函数有关。

How do I solve this issue ? 我该如何解决这个问题?

Your problem is with 你的问题是

union {
    OptionA optA;
    OptionB optB;
}Options;

in Inner . Inner Both OptionA and OptionB are not trivially constructable so when you make a union of them it deletes the constructor of the union. OptionAOptionB是简单的可构造的,所以当你将它们联合起来时,它会删除union的构造函数。 This means Options is not default constructable and therefore Inner is not either since you don't provide a default constructor. 这意味着Options不是默认构造的,因此Inner不是因为您没有提供默认构造函数。

You are going to need to provide your own constructor and destructor for the union to handle constructing and destructing the proper member. 您将需要为union提供自己的构造函数和析构函数来处理构造和破坏正确的成员。 You can see how to make a tagged union to properly destruct the union here 您可以在此处查看如何使标记的联合正确地破坏联合

According to this : 根据这个

If a union contains a non-static data member with a non-trivial special member function (copy/move constructor, copy/move assignment, or destructor), that function is deleted by default in the union and needs to be defined explicitly by the programmer. 如果一个union包含一个带有非平凡特殊成员函数的非静态数据成员(复制/移动构造函数,复制/移动赋值或析构函数),那么该函数在联合中默认被删除,需要由程序员。

So you need to define both constructor and destructor for your union in Inner . 因此,您需要为Inner的union定义构造函数和析构函数。 But because it is an anonymous union, you have to give it a name first: 但因为它是一个匿名联盟,你必须先给它一个名字:

struct Inner
{
    int b;
    union A {
        A();
        ~A();
        OptionA optA;
        OptionB optB;
    } Options;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++错误C2280:尝试引用已删除的函数 - C++ Error C2280: Attempting to reference a deleted function C2280尝试引用已删除的功能 - C2280 attempting to reference a deleted function C2280 = 试图引用已删除的 function - C2280 = attempting to reference a deleted function 错误C2280:尝试引用已删除的功能 - error C2280: attempting to reference a deleted function Qt编译错误:C2280:尝试引用已删除的函数 - Qt compile error: C2280: attempting to reference a deleted function 错误C2280:尝试引用已删除的函数(原子 <int> ) - error C2280: attempting to reference a deleted function (atomic<int>) 编译器错误C2280,尝试引用已删除的函数operator = - Compiler error C2280, attempting to reference a deleted function operator= Visual Studio 2013 和 2015 中的 C++ 编译器错误 C2280“试图引用已删除的函数” - C++ Compiler Error C2280 "attempting to reference a deleted function" in Visual Studio 2013 and 2015 C++ 错误 C2280 - 尝试引用已删除的 Function - 在原始类型上 - C++ Error C2280 - Attempting to Reference a Deleted Function - On Primitive Types C2280:尝试引用已删除的函数(union,struct,copy constructor) - C2280: attempting to reference a deleted function (union, struct, copy constructor)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM