473,382 Members | 1,349 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Problem while using anonymous union in C++

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
3 7692
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Stephen Miller | last post by:
Firstly, sorry for the long post, but I've included a fair bit of sample data. Im doing a comparision of 10yr Bond prices and CPI adjustments, with an 18 week moving average of the CPI. I'm...
6
by: Bill | last post by:
I've created this: SELECT c.ProjectID, Count(c.ID) as 'Registrants', Count(dt.Hits) as 'Submissions' FROM CME_TBL c JOIN (SELECT ProjectID, Count(*) as Hits FROM CME_TBL
3
by: DarthMacgyver | last post by:
Hello, I recently wrote a survey application. Each question is very similar. The first questions gives me a problem when there are multiple people taking the survey (The Database connection...
5
by: JKop | last post by:
You know how from time to time, you want to have an array which you can access via: array_name; But also, you'd like the more user-friendly option: array_name.element1 array_name.element2
5
by: Martin Vorbrodt | last post by:
here's what i have: struct SCSI_CDB { .... union { struct { unsigned char address0; unsigned char address1; unsigned char address2;
1
by: Michael B Allen | last post by:
Consider the following structure with a union: struct shape { int type; union { struct circle c; struct square s; struct polydoodle p; } u; };
1
by: qwerty2_reverse_iterator | last post by:
Is this a bug with the ms compiler (V7.1)? (It seems so at least.) I get errors when I don't initialize all the const pointer fields of an anonymous union in a struct. Example: //T2.h...
6
by: steve.kim | last post by:
Hello, I'm trying to make a class like below... class myClass { public: // ctor / dtor .... // methods
7
by: David Resnick | last post by:
I'm faced with a header with anonymous structures declared inside a union like this: union msg { struct { int a; int b; } s1; struct {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.