[英]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.