Problems with constructors in nested structs and unions
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Problems with constructors in nested structs and unions

  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::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;


    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 10:37 AM.

  2. #2
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    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

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,071

    Re: Problems with constructors in nested structs and unions

    Quote Originally Posted by Gutzy View Post
    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;
    Compiler Error C2620 article says:
    A union member cannot have a default constructor.
    Best regards,
    Igor

  4. #4
    Join Date
    Apr 2011
    Posts
    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?

  5. #5
    Ejaz's Avatar
    Ejaz is offline Elite Member Power Poster
    Join Date
    Jul 2002
    Location
    Lahore, Pakistan
    Posts
    4,211

    Re: Problems with constructors in nested structs and unions

    Quote Originally Posted by Gutzy View Post
    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.

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,016

    Re: Problems with constructors in nested structs and unions

    Quote Originally Posted by Gutzy View Post
    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center