【问题标题】:Problem initializing array of struct{enum;union;}初始化 struct{enum;union;} 数组的问题
【发布时间】:2020-03-16 19:34:07
【问题描述】:

我在定义 s_arr[10] 时遇到问题,它应该是一个结构对象数组,而该结构包含一个枚举和一个联合成员:

enum E {ENUM_A,ENUM_B};
union U {
  String s;
  char c[4];
};
struct S {
  E e;
  U u;
};

S s_arr[10];

我尝试以不同的方式解决这个问题,但都没有奏效。 基本上我得到以下编译错误:

sketch_mar16a:13:11: error: use of deleted function 'S::S()'
 S s_arr[10];
           ^
/.../sketch_mar16a.ino:8:8: note: 'S::S()' is implicitly deleted because the default definition would be ill-formed:
 struct S {
        ^
sketch_mar16a:8:8: error: use of deleted function 'U::U()'
/.../sketch_mar16a.ino:4:7: note: 'U::U()' is implicitly deleted because the default definition would be ill-formed:
 union U {
       ^
sketch_mar16a:5:10: error: union member 'U::s' with non-trivial 'String::String(const char*)'
   String s;
          ^
sketch_mar16a:8:8: error: use of deleted function 'U::~U()'
 struct S {
        ^
/.../sketch_mar16a/sketch_mar16a.ino:4:7: note: 'U::~U()' is implicitly deleted because the default definition would be ill-formed:
 union U {
       ^
sketch_mar16a:5:10: error: union member 'U::s' with non-trivial 'String::~String()'
   String s;
          ^
sketch_mar16a:13:11: error: use of deleted function 'S::~S()'
 S s_arr[10];
           ^
/.../sketch_mar16a/sketch_mar16a.ino:8:8: note: 'S::~S()' is implicitly deleted because the default definition would be ill-formed:
 struct S {
        ^
sketch_mar16a:8:8: error: use of deleted function 'U::~U()'
/.../sketch_mar16a.ino: In function 'void __static_initialization_and_destruction_0(int, int)':
sketch_mar16a:13:3: error: use of deleted function 'S::~S()'
 S s_arr[10];
   ^
exit status 1
use of deleted function 'S::S()'

不幸的是,我不明白错误消息试图指向我的位置。我试图为所涉及的类定义初始化程序,但我不知道如何(或在哪里)为联合定义初始化程序。

使用谷歌,我只发现了联合包含结构的情况,但我无法为我的问题得出解决方案。

您能解释一下,为什么“默认定义格式不正确”?默认定义会是什么样子?在编译期间它是什么时候发生的? 注意:代码是使用 Arduino IDE 为 arduino uno 编译的

【问题讨论】:

  • 也许这是你的编译器特有的东西,但你能告诉我String 是什么吗?
  • @AdrianMole String 是一个用于字符串的 arduino 类。我尝试了不同的数据类型,但没有区别......
  • 可以删除默认构造函数的原因有很多。详情请见timsong-cpp.github.io/cppwp/n3337/class.ctor#5
  • @RSahu 好吧,就我而言,构造函数被删除“因为默认定义格式不正确”,我不明白这告诉我什么......

标签: c++ struct initialization union


【解决方案1】:

根据以下错误信息判断:

sketch_mar16a:8:8: error: use of deleted function 'U::U()'
/.../sketch_mar16a.ino:4:7: note: 'U::U()' is implicitly deleted because the default definition would be ill-formed:
 union U {

/.../sketch_mar16a/sketch_mar16a.ino:4:7: note: 'U::~U()' is implicitly deleted because the default definition would be ill-formed:
 union U {

String s;U 中的存在是问题的根源。您需要在 U 中定义默认构造函数和析构函数,并确保在这些函数中执行正确的操作。

以下简化程序在我的机器上构建。

struct String
{
   String(char const*) {}
   ~String() {}
};

enum E {ENUM_A,ENUM_B};
union U {
   U() : s(nullptr) {}
   ~U() {}
  String s;
  char c[4];
};
struct S {
  E e;
  U u;
};

int main()
{
   S s_arr[10];
}

您需要正确实现U::U()U::~U(),这样您的程序才能正常运行。

【讨论】:

    猜你喜欢
    • 2017-07-21
    • 2021-09-21
    • 1970-01-01
    • 2019-03-14
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    相关资源
    最近更新 更多