Problem while using anonymous union in C++

S

SRK

Hi,
I wanted to use an anonymous union within an structure something like
below -

struct Test
{
union
{
std::string user; //char user[50];
std::string role; //char role[50];
};
std::string desc;
};

Whenever I use, built in data types such as int, char etc, it works
perfectly fine, but for user defined dataypes, it gives me error -

error: member `std::string Test::<anonymous union>::user' with
constructor not allowed in union
error: member `std::string Test::<anonymous union>::user' with
destructor not allowed in union
error: member `std::string Test::<anonymous union>::user' with copy
assignment operator not allowed in union
error: member `std::string Test::<anonymous union>::role' with
constructor not allowed in union
error: member `std::string Test::<anonymous union>::role' with
destructor not allowed in union
error: member `std::string Test::<anonymous union>::role' with copy
assignment operator not allowed in union

Can someone give any idea, how to sort out the problem?

Thanks in advance.

- Shiv Ranjan
(e-mail address removed)
 
A

anon

SRK said:
Hi,
I wanted to use an anonymous union within an structure something like
below -

struct Test
{
union
{
std::string user; //char user[50];
std::string role; //char role[50];
};
std::string desc;
};

This is not allowed (using std::string in a union)
Whenever I use, built in data types such as int, char etc, it works
perfectly fine, but for user defined dataypes, it gives me error -

error: member `std::string Test::<anonymous union>::user' with
constructor not allowed in union
error: member `std::string Test::<anonymous union>::user' with
destructor not allowed in union
error: member `std::string Test::<anonymous union>::user' with copy
assignment operator not allowed in union
error: member `std::string Test::<anonymous union>::role' with
constructor not allowed in union
error: member `std::string Test::<anonymous union>::role' with
destructor not allowed in union
error: member `std::string Test::<anonymous union>::role' with copy
assignment operator not allowed in union

Can someone give any idea, how to sort out the problem?

Listen what your compiler is telling you
 
S

SRK

SRK said:
Hi,
I wanted to use an anonymous union within an structure something like
below -
struct Test
{
    union
    {
        std::string user; //char user[50];
        std::string role;  //char role[50];
    };
    std::string desc;
};

This is not allowed (using std::string in a union)




Whenever I use, built in data types such as int, char etc, it works
perfectly fine, but for user defined dataypes, it gives me error -
error: member `std::string Test::<anonymous union>::user' with
constructor not allowed in union
error: member `std::string Test::<anonymous union>::user' with
destructor not allowed in union
error: member `std::string Test::<anonymous union>::user' with copy
assignment operator not allowed in union
error: member `std::string Test::<anonymous union>::role' with
constructor not allowed in union
error: member `std::string Test::<anonymous union>::role' with
destructor not allowed in union
error: member `std::string Test::<anonymous union>::role' with copy
assignment operator not allowed in union
Can someone give any idea, how to sort out the problem?

Listen what your compiler is telling you

I understand what the compiler is saying and thats why I wanted
whether there is any workaround for that or not?

- Shiv
 
D

dertopper

SRK said:
Hi,
I wanted to use an anonymous union within an structure something like
below -
struct Test
{
    union
    {
        std::string user; //char user[50];
        std::string role;  //char role[50];
    };
    std::string desc;
};
This is not allowed (using std::string in a union)
[snipped error messages]
Listen what your compiler is telling you

I understand what the compiler is saying and thats why I wanted
whether there is any workaround for that or not?

There's no workaround for this. Why don't you use the following:

class Test
{
private:
std::string user_or_role;
public:
std::string& get_user ()
{
return user_or_role;
}

std::string& get_role ()
{
return user_or_role;
}
std::string desc;
};

Regards,
Stuart
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top