I know the error is due to string tom but im not sure how to solve it.does union not take strings or I forgot to include something?
also there is an output where it displays a box with a question mark inside.what is that?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
#include<string>
usingnamespace std;
int main()
{
union bob{
int tim;
char dam[12] = "hithere";
string tom;
}obj;
obj.tim = 6;
for(int x = 0;x<12;x++)
{
cout<<obj.dam[x];
}
}
From “The C++ Programming Language” - Fourth Edition, chapter 8.3.1:
Bjarne Stroustrup wrote:
If a union has a member with a user-defined constructor, a copy operation, a move operation, or a destructor, then that special function is deleted (§3.3.4, §17.6.4) for that union; that is, it cannot be used for an object of the union type.