Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8246611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:33:45+00:00 2026-06-07T22:33:45+00:00

I have a struct nested winin a class and a union of that struct.

  • 0

I have a struct nested winin 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.

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'

};

Following the answers below I have now tried the following, but I get error 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;
};
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:33:47+00:00Added an answer on June 7, 2026 at 10:33 pm

    Others have answered the question about the union; it can’t contain members with non-trivial default constructors. To answer the second question, about member initialisation:

    You can’t initialise non-static members in the class definition; they have to be initialised in the constructor’s initialiser list:

    class test {
        // stuff
        dtType judgement_day;
    };
    
    test::test() : judgement_day(1) {}
    

    The reason that dtType judgement_day(); compiles is that it’s declaring a function, not a variable.

    UPDATE: since you actually want these members to be static, they are instead initialised in a source file, similarly to global variables:

    // in the header
    class test {
        // stuff
        const static dtType judgement_day;
    };
    
    // in exactly one source file
    const test::dtType dtType::judgement_day(1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that uses a nested class, and want to use the
I have a struct that's inside an array that is nested inside another struct,
I have gotten into the habit of declaring structs nested inside the class that
I have a struct that I use in header file1. I now also need
I have a struct, MyStruct , that has a private member private bool[] boolArray;
I have a struct that contains three lists and an integer as can be
I have a struct that looks something like: struct foo_t { template <std::size_t x,
I have a std::vector that holds a Point struct (x,y,z and some other non-pointer
I have 2 structures that have 90% of their fields the same. I want
i have a nested struct and i'd like to have a pointer-to-member to one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.