Problems with constructors in nested structs and unions
I have a struct nested within a class and a union of that struct. If I have a struct constructor that takes arguments, then the union won't compile.
I also want to create an instance of the struct using an argument. That line also fails.
Also if I try to create an instance using braces to initialise it fails.
class test
{
public:
test(void);
~test(void);
struct dtType {
// inline constructors with initialisation lists
dtType() : mins(0), hrs(0),day(0),mnth(0),year(0),DPOffset(0),DTType(0) {}
dtType(byte z) : mins(z), hrs(z),day(z),mnth(z),year(z),DPOffset(0),DTType(0) {}
dtType(byte n,byte h, byte d, byte m, byte y, byte o, byte t) : mins(n), hrs(h),day(d),mnth(m),year(y),DPOffset(o),DTType(t) {}
// if I comment out the union it compiles, otherwise I get:
// error C2620: member 'test::dtUnion::dt' of union 'test::dtUnion' has user-defined constructor or non-trivial default constructor
union dtUnion {
dtType dt;
unsigned long dtLong; // 32 bits
} dtU;
Re: Problems with constructors in nested structs and unions
Please use code tags when posting code to preserve the indentation.
[QUOTE=Gutzy;2076963]
Code:
class test
{
//...
dtType judgement_day; // compiles OK
dtType judgement_day2(1); // error C2059: syntax error : 'constant'
dtType judgement_day3 = {1,1,1,1,1,0,0}; // error C2334: unexpected token(s) preceding '{'; skipping apparent function body
};
You have listed the 'judgement_day' things as members of your class. Therefore, judgement_day2 is interpreted as a member function of class test. If you want to have judgement_day2 as a member variable of class test, then you should define it similar to judgement_day, and initialize it in the constructor of test.
Cheers, D Drmmr
Please put [code][/code] tags around your code to preserve indentation and make it more readable.
As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky
Re: Problems with constructors in nested structs and unions
Originally Posted by Gutzy
I have a struct nested within a class and a union of that struct. If I have a struct constructor that takes arguments, then the union won't compile.
// if I comment out the union it compiles, otherwise I get:
// error C2620: member 'test::dtUnion::dt' of union 'test::dtUnion' has user-defined constructor or non-trivial default constructor
union dtUnion {
dtType dt;
unsigned long dtLong; // 32 bits
} dtU;
Bookmarks