当试图获取系统时间时,访问冲突读取位置



下面是我尝试获取时间的代码片段:

void GetDate(int &month,int &day,int &year){
  time_t SystemTime;
  struct tm *OSTime;
  OSTime=localtime(&SystemTime);
  month=OSTime->tm_mon;
  day=OSTime->tm_mday;
  year=OSTime->tm_year;
  month+=1;
  year+=1900;
}

当到达:

月= OSTime -> tm_mon;

它抛出:

未处理的异常在GonzalesP2.exe 0x01101123: 0xC0000005:访问违反读取位置0x00000010.

下面是调用GetDate的方法:

void Calendar::SetMonthYear(int mon,string sYr){
  string temp;
  GetDate(tMonth,tDay,tYear);
  if(mon==0 && sYr=="0000"){
    mon=tMonth;
    sYr=tYear;
  }
  month=mon;
  //get the year
  temp=sYear.at(2);
  temp+=sYear.at(3);
  year=atoi(temp.c_str());
  //get the century
  temp=sYear.at(0);
  temp+=sYear.at(1);
  cent=atoi(temp.c_str());
  FillMonthGrid();
}

请帮。

你又忘了一步:

  time (&SystemTime); //^^Should first do this
  struct tm *OSTime;
  OSTime=localtime(&SystemTime);

您可以在这里找到一个示例用法:localtime,在调用localtime之前调用time

最新更新