# non POD in a union

This topic is 572 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I'm just trying to get my code from visual studio that compiles fine with cl.exe to compile with NDK, one of the errors I'm getting is:

error : anonymous struct member has a non-trivial constructor
note : because type 'CoPoint2I' has no default constructor
note : implicit default constructor suppressed by user-declared constructor

I've had this type of error before (can't remember how I dealt with last time, by bodging I suspect lol). The thing is, I know that my class is plain old data (POD), I just have a convenience constructor to initialize it in a small minority of cases:

CoPoint2I(int x, int y)

I can see why this might warrant a warning (and indeed visual studio doesn't seem to even flag it as a warning), but the NDK treats it as an error??  :wacko:  Is this because the standard can't guarantee that the object won't have extra house keeping bytes (like a class with virtual functions and a vtable)?  :mellow:

I can of course get rid of the constructor and put in a Set function and replace any calls to it, but is this the only course of action?

##### Share on other sites

Make a default constructor as well (I hope that solves the problem)

Edited by Alberth

##### Share on other sites

Make a default constructor as well (I hope that solves the problem)

I have a feeling, that has solved some similar problems in the past..

but not in this case.. With a default constructor (that does nothing) the error then becomes:

error : anonymous struct member has a non-trivial constructor
note : because type 'CoPoint2I' has a user-provided default constructor

Another alternative I could use is a binary compatible class, then casting when getting from the union. But this seems an ugly hack.

##### Share on other sites

Ahha, potentially solved. A little research and it turns out that allowing non-POD in a union may have changed for c++ 11 (or so). And I had taken out the c++11 switch in the NDK compiler for a test, and forgotten to put it back in. I'm hoping this will solve it (quite a few more errors to work through first though).  :)

##### Share on other sites

Unions have always had problems with non-POD data.

There are some special cases where non-POD types can be used in a union, both before and after C++11. The changes were fairly minor between the two standards.

For most cases where a non-POD union would be useful it is often easier to go with either a small class hierarchy or with a class that has conversion operators to the types you care about.

1. 1
2. 2
3. 3
Rutin
20
4. 4
frob
18
5. 5

• 32
• 13
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
632562
• Total Posts
3007089

×