c++ - Access violation reading location 尝试获取系统时间时

标签 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;
}

当它到达:

month=OSTime->tm_mon;

它抛出:

Unhandled exception at 0x01101123 in GonzalesP2.exe: 0xC0000005: Access violation reading location 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

关于c++ - Access violation reading location 尝试获取系统时间时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16951895/

相关文章:

c++ - C/C++ 中的两个 'main' 函数

c++ - QTcpSocket 数据传输在读取缓冲区已满时停止,并且在释放时不会恢复

c# - 从 C# 类库导出函数

javascript - 从 C++ 调用 javascript 函数

c++ - 我需要手动声明 >= 和 <= 运算符吗?

C++ 头函数

c++ - 行进立方体,非常小的三角形

c++ - GCC 交叉编译为 i586 架构 (Vortex86DX)

c++ - vector 可变范围的最佳实践

c++ - glm::ortho() 实际上是错误的吗?