我有一个嵌套在一个类中的结构和该结构的一个联合。如果我有一个带参数的结构构造函数,那么联合将不会编译。
我还想使用参数创建结构的实例。那条线也失败了。
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::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;
// if I call dtType judgement_day(); it compiles. Otherwise I get:
dtType judgement_day(1); // error C2059: syntax error : 'constant'
};
按照下面的答案,我现在尝试了以下方法,但出现错误 C2438:judgement_day,dateMask,timeMask : cannot initialize static class data via constructor
class test
{
public:
test(): judgement_day(1),dateMask(1,1,1,1,1,0,0),timeMask(1,1,0,0,0,0,0){}
~test();
public:
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;
};
const static dtType judgement_day;
const static dtType dateMask;
const static dtType timeMask;
};