C++ C++;checkDay函数中的Time类没有编译错误、执行停止和应用程序崩溃错误

C++ C++;checkDay函数中的Time类没有编译错误、执行停止和应用程序崩溃错误,c++,C++,下面是日期。h,日期。cpp和main.cpp 我没有得到任何编译错误。但是exe的运行会停止,删除日期类的checkday函数时不会发生这种情况。这个函数有什么问题?我使用DEV C++ +/P> #ifndef DATE_H #define DATE_H class Date{ private: int day, month, year; public: Date(int = 1, int = 1, int = 1990);

下面是日期。h,日期。cpp和main.cpp 我没有得到任何编译错误。但是exe的运行会停止,删除日期类的checkday函数时不会发生这种情况。这个函数有什么问题?我使用DEV C++ +/P>
#ifndef DATE_H
#define DATE_H

class Date{
  private: 
           int day, month, year;
  public:
           Date(int = 1, int = 1, int = 1990);
           ~Date();
           void setDay(int);
           int checkDay(int); //return int i.e. default day 1 or correct date inputted :)
           void setMonth(int);
           void setYear(int);
           int getDay() const;
           int getMonth() const;
           int getYear() const;
           void printDate() const;
};

#endif

#include <iostream>
#include <cmath>
using namespace std;
#include "date.h"

Date::Date(int d, int m, int y){

           //setDay(d); calling setDay() here caused crash
           setMonth(m);
           setYear(y);
           setDay(d);
}
 Date::~Date(){

          cout<<"Date destructor called...\n";
}
void Date::setDay(int d){

          day = checkDay ( d ) ;
}
int Date::checkDay(int dd){ /*COMMENTING THIS FUNCTION REMOVES RUN TIME   ERROR */

          static int daysInmonth[13] = {0,31,28,31,30,31,30,31,31,30,31,30,31};
          if ( dd > 0 && dd <= daysInmonth[month] )
             return dd;
          if( ( year%400==0||( year%100!=0 && year%4==0))&& month==2 && dd==29) 
             return dd;               
          return 1;        
}

void Date::setMonth(int m){

          month = ((m > 0 && m <=12) ? m : 1 );
}
void Date::setYear(int y){

          if (((int)log10((double)y) + 1)!=4) //ild1 != 4
             year = 1990;
          else year = y;
}
int Date::getDay() const{

          return day;
}
int Date::getMonth() const{

          return month;
}
int Date::getYear() const{

          return year;
} 

void Date::printDate() const{

          cout<<"Date: "<<getDay()<<"/"<<getMonth()<<"/"<<getYear()<<endl;
}



#include <cstdlib>    
#include <iostream>
#include "date.h"
using namespace std;

int main()
{

    Date date1( 2, 3, 2004),date2,date3( 2, 1, -1980), 
      date4(0, 12, 2990),date5(29, 2, 2001), date6(12,12, 98686);

cout<<"User inputted valid date: "; date1.printDate();
cout<<"\nDefault valid date: "; date2.printDate();
cout<<"\nAttempt to input invalid date: 2/1/-1980 "; date3.printDate();
cout<<"\nAttempt to input invalid date: 0/12/2990 "; date4.printDate();
cout<<"\nAttempt to input invalid date: 29/2/2001 ( non leap year) "; date5.printDate();   
cout<<"\nAttempt to input invalid date: 12/12/98686 "; date6.printDate();  

cout<<endl;
system("PAUSE");
return EXIT_SUCCESS;
}
#如果取消日期#
#定义日期
上课日期{
私人:
整数日、月、年;
公众:
日期(int=1,int=1,int=1990);
~Date();
无效设定日(int);
int checkDay(int);//返回int,即默认的第1天或输入的正确日期:)
无效月数(整数);
无效年份(整数);
int getDay()常量;
int getMonth()常量;
int getYear()常量;
无效打印日期()常量;
};
#恩迪夫
#包括
#包括
使用名称空间std;
#包括“date.h”
日期:日期(整数d,整数m,整数y){
//setDay(d);在此处调用setDay()导致崩溃
设定月(m);
设置年份(y);
设定日(d);
}
日期::~Date(){

cout我看到的
checkDay()
方法可能崩溃的唯一一点是,如果对
daysInmonth[month]
的访问超出范围

当执行第一个
if
语句时,您是否检查了
月的值是多少?

在checkDay()中,您检查的是月份和年份,但在日期构造函数中,您从尚未分配月份和年份的第一天开始

更改此项:

Date::Date(int d, int m, int y){

       setDay(d);
       setMonth(m);
       setYear(y);
}
为此:

Date::Date(int d, int m, int y){       
       setMonth(m);
       setYear(y);
       setDay(d);
}

你试过调试吗?