简体   繁体   English

访问联合的数据成员时出错

[英]An error in accessing a data member of a union

I keep getting an error message "error: incompatible types in assignment of 'const char[7] to [30]" when accessing the employee name.访问员工姓名时,我不断收到错误消息“错误:'const char[7] 到 [30] 的分配中的类型不兼容”。 I know that only one data member of the union can be accessed at one time.我知道一次只能访问联合的一个数据成员。 Please, help.请帮忙。 Thank you: Here is my code:谢谢:这是我的代码:

#include <iostream>
#include <string>
using namespace std;

union employee
{
    char name[30];
    int ID;
    float salary;
};
int main()
{
    union employee emp; 
    emp.name="Tibebu";
    cout<<"Employee name: "<<emp.name<<endl;
    return 0;
}

The problem isn't that the object is a member of a union.问题不在于 object 是工会的成员。 The problem is that you're trying to assign an array.问题是您正在尝试分配一个数组。 And arrays are not assignable.并且 arrays 不可分配。

To modify an array, you must assign its elements.要修改数组,您必须分配其元素。 There are standard functions for copying multiple elements between arrays. arrays 之间有复制多个元素的标准函数。

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

相关问题 在常量表达式中访问联合成员时出错 - Error in Accessing a Union Member in Constant Expression 访问不活动的工会成员和未定义的行为? - Accessing inactive union member and undefined behavior? 访问类中的结构和联合成员函数 - Accessing struct and union member functions within a class 以原子方式减少联合的数据成员? - Atomically decrement data member of a union? 联合类型的数据成员的初始化 - Initialization of a data member of union type 模板化派生类时访问基本成员数据错误 - Accessing base member data error when derived class is templated 通过限定名访问成员数据的地址会产生错误 - Accessing the address of a member data through qualified name yields error 访问静态原子数据成员会导致链接器错误 - Accessing a static atomic data member causes a linker error 是否正在访问一个联合中的一个成员,该联合是从具有未定义或未指定的另一个成员集的联合复制的? - Is accessing one member in a union that copied from a union with another member set undefined or unspecified? 联合:从联合的一个数据成员读取以写入另一个 - Union: Reading from one data member of a union to write into another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM