Why can't constructors be called as followed

#include <iostream>
#include <string>

using namespace std;

class DayOfYear
{
public:
DayOfYear();
// initializes a null constructor
DayOfYear(int monthValue);
// initializes the month to argument
DayOfYear(int monthValue, int dayValue);
// initializes the month and day to arguments
DayOfYear(int monthValue, int dayValue, int yearValue);
// initializes the month and day and year to arguments

string message;

void input();
void output();
void set(int newMonth);
void set(int newMonth, int newDay);
void set(int newMonth, int newDay, int newYear);
int getMonth();
int getDay();
int getYear();

private:
int month;
int day;
int year;
};

void input();

int main()
{
input();
return 0;
};

DayOfYear::DayOfYear()
{
message = "You did not enter a value\n";
};

DayOfYear::DayOfYear(int monthValue)
{
message = "You entered the following values\n";
month=monthValue;
};

DayOfYear::DayOfYear(int monthValue, int dayValue)
{
message = "You entered the following values\n";
month=monthValue;
day=dayValue;
};

DayOfYear::DayOfYear(int monthValue, int dayValue, int yearValue)
{
message = "You entered the following values\n";
month=monthValue;
day=dayValue;
year=yearValue;
};

int DayOfYear::getMonth()
{
int theMonth = month;

return theMonth;
}

int DayOfYear::getDay()
{
int theDay = day;

return theDay;
}

int DayOfYear::getYear()
{
int theYear = year;

return theYear;
}

void input()
{
int monthV, dayV, yearV;
DayOfYear today( monthV, dayV, yearV );

cout << "Enter today's date in the form MM DD YYYY: ";
cin >> monthV, dayV, yearV;

if( monthV==0 && dayV==0 && yearV==0 )
{
DayOfYear today;
}
else if((monthV>0 && monthV<=12)
&& dayV==0 && yearV==0)
{
DayOfYear today(monthV);
}
else if((monthV>0 && monthV<=12)
&& (dayV>0 and dayV<31) && yearV==0)
{
DayOfYear today(monthV, dayV);
}
else if((monthV>0 && monthV<=12)
&& (dayV>0 and dayV<31)
&& ( yearV>0) )
{
DayOfYear today(monthV, dayV, yearV);
};

cout << today.message
<< "Month: " << today.getMonth() << endl
<< "Day: " << today.getDay() << endl
<< "Year: " << today.getYear() << endl;
};

I tried to build this from an example out of the book (a.k.a. reaching outside my bounds)... I will be straight up. I am reading a great book but for some reason, I can't get it plus my professor is more secure about sharing her issues wanting to build a house 1k away than she is wanting to focus on teaching how to program. More points to making me feel like an idiot in a respectful way.

-J
The set functions were in the example but were not defined. I purposefully left them undefined because I wanted to explore constructors. When do overloaded constructors take precedence and and when do they matter?
Ok, what is the actual question?
Your problem is that of scope.

1
2
3
4
5
6
7
8
9
if( monthV==0 && dayV==0 && yearV==0 )
{
  DayOfYear today;  // this properly calls the default constructor
}  // but then then your 'today' object dies here because its scope ends

//...

cout << today.message  // so when you try to use it here, the compiler errors because 'today'
    // no longer exists 


Remember that objects only have lifetime for the {braces} that they are enclosed in.
Last edited on
Thank you. Makes more sense now.
Topic archived. No new replies allowed.