C++ Union compilation errors

S

Sujal

In below program, I'm getting below compilation errors

error C2621: member '_tag_nodes_::node_' of union '_tag_nodes_' has
copy constructor
error C2621: member '_tag_nodes_::linkednode_' of union '_tag_nodes_'
has copy constructor

Can anybody help me resolve this or any workaround for this?
I must need union because in my data structure only one type of node
is possible at time. So I have taken union. And ya my data structure
is still very big but i posted a fraction part here to simplify the
problem.

#include "stdafx.h"
#include <stdio.h>
#include<iostream>
#include<conio.h>
#include <list>
using namespace std;

typedef struct _tag_nodedata
{
std::string name;
std::string type;

}NODEDATAINFO;
typedef list<NODEDATAINFO> NODEDATA;
typedef struct _tag_nodeinfo
{
void *_nodeptr;
string path_;
NODEDATA data_;

}NODEINFO;
typedef struct _tag_linkednodeinfo
{
void *_nodeptr;
string linkpath_;
int queued;
int hidden;

}LINKEDNODEINFO;

typedef union _tag_nodes_
{
NODEINFO nodeinfo_;
LINKEDNODEINFO linkednode_;

}NODES;
typedef struct _unrsolvednode_
{
NODES node;
int type;
}UNRESOLVED;

int _tmain(int argc, _TCHAR* argv[])
{
getch();
}
 
I

Ian Collins

Sujal said:
In below program, I'm getting below compilation errors

error C2621: member '_tag_nodes_::node_' of union '_tag_nodes_' has
copy constructor
error C2621: member '_tag_nodes_::linkednode_' of union '_tag_nodes_'
has copy constructor
It's telling you can can't put something with a constructor or
destructor in a union.
typedef struct _tag_nodedata

You don't need the typedef in C++.
{
std::string name;
std::string type;

}NODEDATAINFO;

All caps for a class name is hideous!
int _tmain(int argc, _TCHAR* argv[])

Why not write

int main(int argc, char* argv[])
 
S

Sujal

Sujal said:
In below program, I'm getting below compilation errors
error C2621: member '_tag_nodes_::node_' of union '_tag_nodes_' has
copy constructor
error C2621: member '_tag_nodes_::linkednode_' of union '_tag_nodes_'
has copy constructor

It's telling you can can't put something with a constructor or
destructor in a union.


typedef struct _tag_nodedata

You don't need the typedef in C++.
{
std::string name;
std::string type;
}NODEDATAINFO;

All caps for a class name is hideous!


int _tmain(int argc, _TCHAR* argv[])

Why not write

int main(int argc, char* argv[])

Hi Ian,
Thanks for even replying..
I have read on internet on Microsoft site that we can put structure in
Union.
Please refer below link....for more information.
http://msdn.microsoft.com/en-us/library/47azs69s.aspx

If I can't put structure type in union then i need to change my entire
data structure...
BTW, I have created temporary project so I haven't changed following
lines " int _tmain(int argc, _TCHAR* argv[])" while posting the
message.

thanks.
 
I

Ian Collins

Sujal wrote:

[please don't quote signatures]
Hi Ian,
Thanks for even replying..
I have read on internet on Microsoft site that we can put structure in
Union.

You can, but the structure may not have a non-trivial constructor, copy
constructor or destructor. See 9.5.1 in the standard.

One of your structures contained std::strings, so they can't be a member
of a union.
 
S

Sujal

Sujal wrote:

[please don't quote signatures]


Hi Ian,
Thanks for even replying..
I have read on internet on Microsoft site that we can put structure in
Union.

You can, but the structure may not have a non-trivial constructor, copy
constructor or destructor.  See 9.5.1 in the standard.

One of your structures contained std::strings, so they can't be a member
of a union.

Hi Ian,
Oh now I got your point. std::strings.... Thanks for that.
Can you please tell me where can I find 9.5.1 Standard. Can you please
share link for that?
Thanks in advance.
 

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

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top