0
votes

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;
}
2
What did you expect Date d (int Day, int Month, int Year); to do? It's not constructor call, it's function declaration. Consider reading a good book to learn from.Algirdas Preidžius

2 Answers

0
votes

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
0
votes

The Reason that this error occurs in your code is because you have Decalred the object d in wrong way: In main function, try replacing Date d (int Day, int Month, int Year);by Date d (1,1,2001);(or pass any integers instead of this).

Well, even after that, i noticed there will be another compile time error in your program. You have called d.printMonth()(in main()) which does not exist in the class. I think you wanted to call d.printMonthFirst()instead.

I hope this solves the problem. Best Of luck !