CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  1. #1
    Join Date
    Apr 2011
    Posts
    4

    Question 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.

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
  •  





Click Here to Expand Forum to Full Width

Featured