Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 25-10-2008
JTD JTD is offline
Member
 
Join Date: May 2008
Posts: 25
Problem with anonymous union in C++

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?
Reply With Quote
  #2  
Old 25-10-2008
Member
 
Join Date: Oct 2008
Posts: 77
Re: Problem with anonymous union in C++

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

class Test
Code:
{
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;
};
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Problem with anonymous union in C++"
Thread Thread Starter Forum Replies Last Post
How to define Union? seema_thk Software Development 5 15-12-2009 02:07 PM
Confused about difference between structure and union rashmi_ay Software Development 3 26-11-2009 01:37 PM
Using Internal and External union SQL Queries Elbanco Software Development 4 06-11-2009 10:45 PM
Differences between UNION and JOINS? Swati Software Development 3 13-02-2009 05:58 PM
Should IT form a union? absolute55 Education Career and Job Discussions 1 05-09-2008 07:49 PM


All times are GMT +5.5. The time now is 04:09 AM.