ksnowman17 ksnowman17 -4 years ago 198
C++ Question

error request for member which is of non class type Date (int,int,int)

I've just started learning about classes and I am having difficulty with using my member functions in my main to print the data I want.
This is the error i'm getting:

main.cpp: In function ‘int main()’:
error: request for member ‘printNumerical’ in ‘d’, which is of non-class type ‘Date(int, int, int)’
d.printNumerical();
^

error: request for member ‘printMonth’ in ‘d’, which is of non-class type ‘Date(int, int, int)’
d.printMonth();
^

error: request for member ‘printDateFirst’ in ‘d’, which is of non-class type ‘Date(int, int, int)’
d.printDateFirst();
^

Here is my main:

int main ()
{
int Day, Month, Year;

cout << "date information: ";
cin >> Day;
cin >> Month;
cin >> Year;

cout << Day << " " << Month << " " << Year << endl;

Date d (int Day, int Month, int Year);

//where I am having issues
d.printNumerical();
d.printMonth();
d.printDateFirst();

return 0;
}


And here is my class definition Date.h

class Date
{
private:
int month,
day,
year;

public:
Date(int Day,int Month,int Year); //constructor
Date(); //constructor if not passed arguments
void printNumerical(); //functions to output in certain format
void printMonthFirst();
void printDateFirst();
};


Here is Date.cpp

Date::Date()
{
month = 1;
day = 1;
year = 2001;
}

Date::Date(int Day, int Month, int Year)
{ //input validation
if((Month < 1) || (Month > 12) || (Day < 1) || (Day > 31) || (Year < 0))
{
month = 1;
day = 1;
year = 2001;
}
else{
month = Month; //accept passed arguments if valid
day = Day;
year = Year;
}
}

void Date::printNumerical()
{
cout << month << "/" << day << "/" << year << endl;
}

void Date::printMonthFirst()
{
switch(month)
{
case 1 : cout << "January ";
break;
case 2 : cout << "February ";
break;
case 3 : cout << "March ";
break;
case 4 : cout << "April ";
break;
case 5 : cout << "May ";
break;
case 6 : cout << "June ";
break;
case 7 : cout << "July ";
break;
case 8 : cout << "August ";
break;
case 9 : cout << "September ";
break;
case 10 : cout << "October ";
break;
case 11 : cout << "November ";
break;
case 12 : cout << "December ";
break;
}
cout << day << ", " ;
cout << year << endl;
}

void Date::printDateFirst()
{
cout << day << " ";
switch(month)
{
case 1 : cout << "January ";
break;
case 2 : cout << "February ";
break;
case 3 : cout << "March ";
break;
case 4 : cout << "April ";
break;
case 5 : cout << "May ";
break;
case 6 : cout << "June ";
break;
case 7 : cout << "July ";
break;
case 8 : cout << "August ";
break;
case 9 : cout << "September ";
break;
case 10 : cout << "October ";
break;
case 11 : cout << "November ";
break;
case 12 : cout << "December ";
break;
}
cout << year << endl;
}

Answer Source

The following line:

Date d (int Day, int Month, int Year);

Actually is a forward declaration of the function taking three integers as input and returning Date object.

Date d (Day, Month, Year); // proper construction
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download