I haee this small piece of code in which I'm trying to create a struct Employee. The employee can be either a manager or worker. I'm having trouble accessing members of the unions. Here's my code
#include <iostream>
#include <string>
using namespace std;
struct Employee {
int id;
union Person {
struct Manager {
int level;
} manager;
struct Worker {
string department;
} worker;
} company[3];
};
int main() {
Employee one;
one.id = 101;
one.company[0].manager.level = 3;
Employee two;
two.id = 102;
two.company[1].worker.department = "Sales";
Employee three;
three.id = 103;
three.company[2].worker.department = "Marketing";
}
The error I get is
arraOfUnions.cc:13:5: error: member 'Employee::Person::Worker Employee::Person::worker' with constructor not allowed in union
arraOfUnions.cc:13:5: error: member 'Employee::Person::Worker Employee::Person::worker' with destructor not allowed in union
arraOfUnions.cc:13:5: error: member 'Employee::Person::Worker Employee::Person::worker' with copy assignment operator not allowed in union
arraOfUnions.cc:13:5: note: unrestricted unions only available with -std=c++0x or -std=gnu++0x
I'm not sure what I'm doing wrong. Please help Thanks SO