【问题标题】:gmtime won't work for a object read with overloaded operatorgmtime 不适用于使用重载运算符读取的对象
【发布时间】:2016-04-27 21:58:51
【问题描述】:

所以我为一个位置创建了一个类,我在其中存储它的坐标和时间,以 UTC 格式。我像这样重载了 >> 运算符

friend ifstream& operator >>(ifstream& in, loc_list& l)
{
    char bf[40];
    in >> bf;
    l.setID(bf);
    long t=0;
    in >> l.utc;
    //l.setTime(t);
    double point;
    in >> point;
    l.p.setX(point);
    in >> point;
    l.p.setY(point);
    in >> l.speed;
    return in;

}

friend ostream& operator <<(ostream& out,const loc_list &l)
{
    out << l.id << endl;
    out << put_time(gmtime(&l.utc),"%c %Z" )<< endl;
    //out << l.utc << endl;
    out << l.p.getX() << endl;
    out << l.p.getY() << endl;
    out << l.speed;
    return out;
}

但是,> 阅读它时,它会中断。 我已经调试了程序,但对象包含正确的数据 print from debugger

那么,有什么想法吗?

【问题讨论】:

  • 它是如何以及具体在哪里中断的?
  • 这里爆发了

标签: c++ oop time time-t


【解决方案1】:

put_time 对应的是get_time

我建议使用:

std::tm tm 
in >> get_time(&tm, "%c %Z");

然后从tm 更新l.utc

l.utc = mktime(&tm);

【讨论】:

  • 我无法像那样读取时间,因为在我的输入文件中,时间看起来像这样 1439467748512
  • @RamonaMihaelaDinescu,我尝试在一个简单的程序中测试功能,但我尝试的编译器似乎不支持std::put_timestd::get_time。您可以在您的环境中尝试一下。链接:ideone.com/QqHoB2.
猜你喜欢
  • 1970-01-01
  • 2013-11-30
  • 1970-01-01
  • 1970-01-01
  • 2019-10-03
  • 2020-01-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多