【发布时间】: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