0

所以我为一个位置创建了一个类,我在其中存储它的坐标和时间,在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;
}

但是, << 运算符中的 gmtime 仅在我使用构造函数创建对象时才起作用。当我用 cin>> 阅读它时,它会中断。我已经调试了程序,但对象包含 来自调试器的正确数据打印

那么,有什么想法吗?

4

1 回答 1

0

的对应物put_timeget_time

我建议使用:

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

然后l.utc从更新tm

l.utc = mktime(&tm);
于 2016-01-21T15:25:40.037 回答