By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,077 Members | 1,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,077 IT Pros & Developers. It's quick & easy.

Problem while using anonymous union in C++

P: n/a
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
ku*****@gmail.com
Oct 21 '08 #1
Share this Question
Share on Google+
3 Replies


P: n/a
SRK wrote:
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
Oct 21 '08 #2

P: n/a
SRK
On Oct 21, 3:06*pm, anon <a...@no.invalidwrote:
SRK wrote:
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
Oct 22 '08 #3

P: n/a
SRK wrote:
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 -
[snipped error messages]
>
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?
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
Oct 22 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.