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

  • SEARCH
  • Home
  • 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 7715695
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:27:07+00:00 2026-06-01T02:27:07+00:00

Edit: Read the lines in bold to find where I’m having problems. I changed

  • 0

Edit: Read the lines in bold to find where I’m having problems. I changed the code after reading the answers, but I’m still getting an error

I’m new to C++ and I’m testing some things to get to know how to use the language and I came across this case:

I have a struct that creates a pointer to another struct. That other struct, in turn, creates a pointer to the struct that instantiated it in the first place.

Here’s a quick example to explain what I mean a little bit better (this part has been changed with added code with lines in bold being where I’m having problems):

#include <string>

using namespace std;

struct Month;
struct Year;

struct Day{
    unsigned int day, dayNumber;
    string name;

    Day(){}

    void setDayNumber(unsigned int dayNumber){
        this -> dayNumber = dayNumber;

        switch(dayNumber){
        case 0:
            name = "Sunday";
            break;
        case 1:
            name = "Monday";
            break;
        case 2:
            name = "Tuesday";
            break;
        case 3:
            name = "Wednesday";
            break;
        case 4:
            name = "Thursday";
            break;
        case 5:
            name = "Friday";
            break;
        case 6:
            name = "Saturday";
            break;
        }
    }
};

struct Month{
    string name;
    unsigned int monthLength, monthNumber;
    Month *previousMonth, *nextMonth;
    Year *year;
    Day *days;

    Month(unsigned int monthNumber, Year *year){
        this -> monthNumber = monthNumber;

        switch(monthNumber){
        case 0:
            name = "January";
            break;
        case 1:
            name = "February";
            break;
        case 2:
            name = "March";
            break;
        case 3:
            name = "April";
            break;
        case 4:
            name = "May";
            break;
        case 5:
            name = "June";
            break;
        case 6:
            name = "July";
            break;
        case 7:
            name = "August";
            break;
        case 8:
            name = "September";
            break;
        case 9:
            name = "October";
            break;
        case 10:
            name = "November";
            break;
        case 11:
            name = "December";
            break;
        }

        previousMonth = NULL;
        nextMonth = NULL;
        this -> year = year;
    }

    void createDays(){
        if(name == "January" || name == "March" || name == "May" || name == "July" || name == "August" || name == "October" || name == "December")
            monthLength = 31;
        else if(name != "February")
            monthLength = 30;
        else{
            **if(year -> isLeapYear() == true)** //This line is problematic, see below the code to see what the compiler says
                monthLength = 29;
            else 
                monthLength = 28;
        }

        days = new Day[monthLength];

        for(unsigned int i = 0; i < monthLength; i++){
            days[i].day = i+1;

            if(i == 0 && previousMonth == NULL)
                days[i].setDayNumber(2);
            else if(i == 0)
                days[i].setDayNumber(previousMonth -> days[previousMonth -> monthLength - 1].dayNumber);
            else
                days[i].setDayNumber(days[i-1].dayNumber);
        }
    }
};

struct Year{
    unsigned int year;
    Year *previousYear, *nextYear;
    Month *months;

    Year(unsigned int year){
        this -> year = year;
        previousYear = NULL;
        nextYear = NULL;
    }

    void createMonths(){
        Month *currentMonth = months;
        unsigned int monthNumber;

        for(unsigned int i = 0; i < 12; i++){
            months = new Month(i, this);
        }

        while(currentMonth != NULL){
            monthNumber = currentMonth -> monthNumber;

            if(monthNumber == 0 && previousYear != NULL){
                months[monthNumber].previousMonth = &(previousYear -> months[11]);
                months[monthNumber].nextMonth = &(months[monthNumber+1]);
            }
            else if(monthNumber == 11 && nextYear != NULL){
                months[monthNumber].nextMonth = &(nextYear -> months[0]);
                months[monthNumber].previousMonth = &(months[monthNumber-1]);
            }
            else{
                if(monthNumber != 0)
                    months[monthNumber].previousMonth = &(months[monthNumber-1]);
                if(monthNumber != 11)
                    months[monthNumber].nextMonth = &(months[monthNumber+1]);
            }
        }

        currentMonth = months;

        while(currentMonth != NULL){
            currentMonth -> createDays();
            currentMonth = currentMonth -> nextMonth;
        }
    }

    bool isLeapYear(){
        if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
            return true;
        else
            return false;
    }
};

Here are the messages the compiler outputs:

pe_019.cpp(107): error C2027: use of undefined type 'Year'
pe_019.cpp(10) : see declaration of 'Year'
pe_019.cpp(107): error C2227: left of '->isLeapYear' must point to class/struct/union/generic type

Obviously, there’s more to it than that, but in Year, I use an array of struct Month, and in Month, there’s a pointer to its parent Year. (Same for Day, is has a parent Month).

When I try to compile, the compiler says I’m using an undefined struct 'Month', that Month* has an unknown size, etc.

I’m pretty sure it’s because Month is used before it is declared, but I can’t declare it first because then Yearwill be the undeclared struct.

What’s the correct way of going about this?

Edit: I forward-declared Year and put Month before Year after reading the answers to this question, but I’m still encountering problems because of a function I hadn’t included in my original post since I didn’t think it was part of the problem, I edited my code to make it more complete.

  • 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-01T02:27:08+00:00Added an answer on June 1, 2026 at 2:27 am

    Your code can be improved in several ways, as follows.

    #include <string>
    using std::string;
    
    struct Year;
    struct Day;
    
    struct Month{
        string name;
        unsigned int monthLength;
        struct Month *previousMonth, *nextMonth;
        struct Year *year;
        struct Day *days;
    };
    
    struct Year{
        unsigned int year;
        struct Year *previousYear, *nextYear;
        struct Month months[12];
    };
    

    The most important problem that this fixes is that, though the definition of Month only specifies a pointer to a Year, the definition of Year actually includes whole Months. Let’s think about that for a moment, because there is something significant to learn here. If the compiler does not yet know what a Month is, how can it decide how to lay out a Year in memory? Answer: it cannot, because it does not even know the size of a Month.

    Merely including a pointer does not bring this problem, because the compiler knows the size of the pointer even if it does not know anything about the type of the object pointed to.

    Your code also needed some semicolons, plus a standard header inclusion, both of which I have added.

    Good luck.

    Update in response to OP’s comment below: Revised code is posted below. Notice that, and how, it delays the definition of the affected function until the type the function needs has been fully defined.

    (Incidentally, if my thoughts in the matter interest you, using namespace std is almost never recommended. It defeats C++’s useful namespace mechanism.)

    #include <string>
    
    using namespace std;
    
    struct Month;
    struct Year;
    
    struct Day{
        unsigned int day, dayNumber;
        string name;
    
        Day(){}
    
        void setDayNumber(unsigned int dayNumber){
            this -> dayNumber = dayNumber;
    
            switch(dayNumber){
            case 0:
                name = "Sunday";
                break;
            case 1:
                name = "Monday";
                break;
            case 2:
                name = "Tuesday";
                break;
            case 3:
                name = "Wednesday";
                break;
            case 4:
                name = "Thursday";
                break;
            case 5:
                name = "Friday";
                break;
            case 6:
                name = "Saturday";
                break;
            }
        }
    };
    
    struct Month{
        string name;
        unsigned int monthLength, monthNumber;
        Month *previousMonth, *nextMonth;
        Year *year;
        Day *days;
    
        Month(unsigned int monthNumber, Year *year){
            this -> monthNumber = monthNumber;
    
            switch(monthNumber){
            case 0:
                name = "January";
                break;
            case 1:
                name = "February";
                break;
            case 2:
                name = "March";
                break;
            case 3:
                name = "April";
                break;
            case 4:
                name = "May";
                break;
            case 5:
                name = "June";
                break;
            case 6:
                name = "July";
                break;
            case 7:
                name = "August";
                break;
            case 8:
                name = "September";
                break;
            case 9:
                name = "October";
                break;
            case 10:
                name = "November";
                break;
            case 11:
                name = "December";
                break;
            }
    
            previousMonth = NULL;
            nextMonth = NULL;
            this -> year = year;
        }
    
        void createDays();
    
    };
    
    struct Year{
        unsigned int year;
        Year *previousYear, *nextYear;
        Month *months;
    
        Year(unsigned int year){
            this -> year = year;
            previousYear = NULL;
            nextYear = NULL;
        }
    
        void createMonths(){
            Month *currentMonth = months;
            unsigned int monthNumber;
    
            for(unsigned int i = 0; i < 12; i++){
                months = new Month(i, this);
            }
    
            while(currentMonth != NULL){
                monthNumber = currentMonth -> monthNumber;
    
                if(monthNumber == 0 && previousYear != NULL){
                    months[monthNumber].previousMonth = &(previousYear -> months[11]);
                    months[monthNumber].nextMonth = &(months[monthNumber+1]);
                }
                else if(monthNumber == 11 && nextYear != NULL){
                    months[monthNumber].nextMonth = &(nextYear -> months[0]);
                    months[monthNumber].previousMonth = &(months[monthNumber-1]);
                }
                else{
                    if(monthNumber != 0)
                        months[monthNumber].previousMonth = &(months[monthNumber-1]);
                    if(monthNumber != 11)
                        months[monthNumber].nextMonth = &(months[monthNumber+1]);
                }
            }
    
            currentMonth = months;
    
            while(currentMonth != NULL){
                currentMonth -> createDays();
                currentMonth = currentMonth -> nextMonth;
            }
        }
    
        bool isLeapYear(){
            if(year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
                return true;
            else
                return false;
        }
    };
    
    void Month::createDays(){
        if(name == "January" || name == "March" || name == "May" || name == "July" || name == "August" || name == "October" || name == "December")
            monthLength = 31;
        else if(name != "February")
            monthLength = 30;
        else{
            if(year -> isLeapYear() == true)
                monthLength = 29;
            else
                monthLength = 28;
        }
    
        days = new Day[monthLength];
    
        for(unsigned int i = 0; i < monthLength; i++){
            days[i].day = i+1;
    
            if(i == 0 && previousMonth == NULL)
                days[i].setDayNumber(2);
            else if(i == 0)
                days[i].setDayNumber(previousMonth -> days[previousMonth -> monthLength - 1].dayNumber);
            else
                days[i].setDayNumber(days[i-1].dayNumber);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use the following block of code to read lines out of a file
I have been reading SO for some time now, but I truly cannot find
I want to read from a file each line and edit only the lines
Here is the code snippet. read = new FileReader(trainfiles/+filenames[i]); br = new BufferedReader(read); while((lines
EDIT: I'm told that making you guys read means I get less attention. My
I've read posts about why you can't have a (Edit -- generic) (which use
FULL EDIT: I urgently need to access a Microsoft SQL Server and read compressed
Edit : Solved, there was a trigger with a loop on the table (read
How can I get and set the 'read-only' property of an edit box?
Got my little mechanize code: br.open('http://tumblr.com/customize'); print br.response().read() print br.form['edit_tumblelog[cname]'] # there definitely is

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.