-
July 17th, 2012, 09:34 AM
#1
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) {}
// overloaded operator functions
bool operator< (dtType date){return true;};
bool operator<= (dtType date){return true;};
bool operator> (dtType date){return true;};
bool operator>= (dtType date){return true;};
bool operator== (dtType date){return true;};
// data members
unsigned mins: 3;
unsigned hrs: 5; // 8 bits
unsigned day: 5;
unsigned mnth: 4;
unsigned year: 7; // 16 bits
unsigned DPOffset: 6;
unsigned DTType : 2;
};
// if I comment out the union it compiles, otherwise I get:
// error C2620: member 'test:tUnion:t' of union 'test:tUnion' has user-defined constructor or non-trivial default constructor
union dtUnion {
dtType dt;
unsigned long dtLong; // 32 bits
} dtU;
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
};
Last edited by Gutzy; July 17th, 2012 at 09:37 AM.
-
July 17th, 2012, 11:24 AM
#2
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
-
July 17th, 2012, 01:59 PM
#3
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:tUnion:t' of union 'test:tUnion' has user-defined constructor or non-trivial default constructor
union dtUnion {
dtType dt;
unsigned long dtLong; // 32 bits
} dtU;
Compiler Error C2620 article says:
A union member cannot have a default constructor.
Best regards,
Igor
-
July 18th, 2012, 02:46 AM
#4
Re: Problems with constructors in nested structs and unions
Thanks for your help. I'll get rid of the union and use a cast. Can you help me on how to get the compiler to let me cast my struct to a long?
-
July 18th, 2012, 03:57 AM
#5
Re: Problems with constructors in nested structs and unions
Originally Posted by Gutzy
Thanks for your help. I'll get rid of the union and use a cast. Can you help me on how to get the compiler to let me cast my struct to a long?
Look into MSDN for reference and example.
-
July 18th, 2012, 07:43 AM
#6
Re: Problems with constructors in nested structs and unions
Originally Posted by Gutzy
Thanks for your help. I'll get rid of the union and use a cast. Can you help me on how to get the compiler to let me cast my struct to a long?
You can use a POD struct to keep the data and store an instance of that struct in your class that you want to cast.
Something like this (not tested):
Code:
struct dtType
{
// c'tors and operators
unsigned long ToLong() const {
union {
data d;
unsigned long l;
} u;
u.d = m_Data;
return u.l;
}
private:
struct data {
unsigned mins: 3;
unsigned hrs: 5; // 8 bits
unsigned day: 5;
unsigned mnth: 4;
unsigned year: 7; // 16 bits
unsigned DPOffset: 6;
unsigned DTType : 2;
} m_Data;
};
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|