访问冲突读取位置尝试获取系统时间时

时间:2013-06-06 00:21:13

标签: c++

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

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();
}

请帮忙。

1 个答案:

答案 0 :(得分:1)

你又忘了一步:

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

您可以在此处找到示例用法:localtime,在致电time之前致电localtime