函数调用说未定义标识符


// Lab 5: Date.h
#ifndef DATE_H
#define DATE_H

class Date 
{
public:
   Date( int = 1, int = 1, int = 2000 ); // default constructor
   void print(); // print function
   void setDate( int, int, int ); // set month, day, year
   void setMonth( int ); // set month
   void setDay( int ); // set day
   void setYear( int ); // set year
   int getMonth(); // get month
   int getDay(); // get day
   int getYear(); // get year 
   void nextday(int &,int &,int&);//Date of accumulation
   /* Write a member function prototype for nextDay,
      which will increment the Date by one day */
private:
   int month; // 1-12
   int day; // 1-31 (except February(leap year), April, June, Sept, Nov)
   int year; // 1900+
   bool leapYear(); // leap year
   int monthDays(); // days in month 
}; // end class Date

#endif


```c++
// Lab 5: Date.cpp
// Member-function definitions for class Date.
#include "Date.h"
#include <iostream> 
using namespace std; 

 // include definition of class Date


Date::Date( int m, int d, int y ) 
{
   setDate( m, d, y ); // sets date 
} // end Date constructor

void Date::setDate( int mo, int dy, int yr )
{
   setMonth( mo ); // invokes function setMonth 
   setDay( dy ); // invokes function setDay
   setYear( yr ); // invokes function setYear
} // end function setDate

void Date::setDay( int d )
{
   if ( month == 2 && leapYear() )  
      day = ( d <= 29 && d >= 1 ) ? d : 1; 
   else
      day = ( d <= monthDays() && d >= 1 ) ? d : 1;
} // end function setDay

void Date::setMonth( int m ) 
{ 
   month = m <= 12 && m >= 1 ? m : 1; // sets month  
} // end function setMonth

void Date::setYear( int y ) 
{
   year = y >= 1900 ? y : 1900; // sets year
} // end function setYear

int Date::getDay() 
{
   return day;
} // end function getDay

int Date::getMonth() 
{ 
   return month; 
} // end function getMonth

int Date::getYear() 
{ 
   return year; 
} // end function getYear


void Date::print()
{
   cout << month << '-' << day << '-' << year << '\n'; // outputs date
} // end function print

/* c */

bool Date::leapYear()
{
   if ( getYear() % 400 == 0 || ( getYear() % 4 == 0 && getYear() % 100 != 0 ) )
         return true; // is a leap year
      else
         return false; // is not a leap year
} // end function leapYear

int Date::monthDays()
{
   const int days[ 12 ] =  
     { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

   return getMonth() == 2 && leapYear() ? 29 : days[ getMonth() - 1 ];
} // end function monthDays
//returns the maximum number of days in the current month
void nextday(int &day,int &month,int &year) {
    day+= 1;
    int maxday = monthDays();//调用monthDays会报错
//未定义标识符
    if (month > maxday) {
        month += 1;
        day = 1;
    }
    if (month == 13) {
        year++;
        month = 1;
    }
}




```c++
// Lab 5: DateTest.cpp
#include <iostream> 
using namespace std; 

#include "Date.h" // include definitions of class Date

int main()
{
   const int MAXDAYS = 16;
   Date d( 12, 24, 2004 ); // instantiate object d of class Date

   // output Date object d's value
   for ( int loop = 1; loop <= MAXDAYS; ++loop ) 
   {
      d.print(); // invokes function print
      /* Write call to nextDay */
      d.nextday();//nextday函数括号里面不知道该怎么引用,d.getDAY吗?
      cout << " The next day will be:";
      d.print();
   } // end for

   cout << endl;
} // end main


包含这个函数声明的头文件 添加进去了吗?

monthDays()是Date的私有成员函数,只可以在Date类中被调用,不可被外部调用。解决办法:把int monthDays(); 放到public里面