473,788 Members | 3,053 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::<anonymou s union>::user' with
constructor not allowed in union
error: member `std::string Test::<anonymou s union>::user' with
destructor not allowed in union
error: member `std::string Test::<anonymou s union>::user' with copy
assignment operator not allowed in union
error: member `std::string Test::<anonymou s union>::role' with
constructor not allowed in union
error: member `std::string Test::<anonymou s union>::role' with
destructor not allowed in union
error: member `std::string Test::<anonymou s 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.c om
Oct 21 '08 #1
3 7725
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::<anonymou s union>::user' with
constructor not allowed in union
error: member `std::string Test::<anonymou s union>::user' with
destructor not allowed in union
error: member `std::string Test::<anonymou s union>::user' with copy
assignment operator not allowed in union
error: member `std::string Test::<anonymou s union>::role' with
constructor not allowed in union
error: member `std::string Test::<anonymou s union>::role' with
destructor not allowed in union
error: member `std::string Test::<anonymou s 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.invali dwrote:
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::<anonymou s union>::user' with
constructor not allowed in union
error: member `std::string Test::<anonymou s union>::user' with
destructor not allowed in union
error: member `std::string Test::<anonymou s union>::user' with copy
assignment operator not allowed in union
error: member `std::string Test::<anonymou s union>::role' with
constructor not allowed in union
error: member `std::string Test::<anonymou s union>::role' with
destructor not allowed in union
error: member `std::string Test::<anonymou s 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
9484
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 using a nested sub-query to calculate the moving average, but I'm having difficulty selecting exactly 18 data points (ie When I include the 'HAVING COUNT(C1.Closes) = 18' line, I get no results). Can anyone help?
6
2744
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
6114
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 Timed out) I am using the Data Access Application Blocks as ASP.NET (using VB.NET) and SQL 2000. In there first question there can be up to 27 answers. So I figured instead of making 27 different trips to the database I woulc just concatenate my...
5
2730
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
14525
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
2341
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
2405
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 typedef int Type1; typedef float Type2; struct S1 {
6
2584
by: steve.kim | last post by:
Hello, I'm trying to make a class like below... class myClass { public: // ctor / dtor .... // methods
7
8865
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 {
0
9655
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10363
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7517
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5535
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.