クラスとその構造体の共用体にネストされた構造体があります。引数を取る構造体コンストラクターがある場合、共用体はコンパイルされません。
また、引数を使用して構造体のインスタンスを作成したいと考えています。その行も失敗します。
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 が表示されます。
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;
};