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.
Your code can be improved in several ways, as follows.
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.)