微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

涉及输出计时持续时间的C ++错误

如何解决涉及输出计时持续时间的C ++错误

这是我正在上课的课程(某些部分被删去了)

const int records = 7;

    class Timed {
    public:

        friend std::ostream& operator<<(std::ostream& os,Timed right) {
            for (int i = 0; i < records; i++) {
                os << right.eR[i].vName << " " << right.eR[i].duration << " " << right.eR[i].seconds << std::endl;
            }
            return os;
        }

    private:

        std::chrono::time_point<std::chrono::steady_clock> start,end;

        struct {
            std::string vName;
            std::string seconds;
            std::chrono::duration<float> duration;
        } eR[records];

    };

基本上,我正在尝试输出匿名结构的值。但是,我得到了错误

binary '<<': no operator found which takes a right-hand operand of type 'std::chrono::duration<float,std::ratio>1,1>>' (or there is no acceptable conversion)

我想知道如何在持续时间内打印该值?预先感谢

解决方法

在C ++ 11/14/17中,没有chrono::duration类型的流运算符。您有两种选择:

  1. 提取.count()

|

 os << right.eR[i].duration.count() << "s ";
  1. 使用此open-source,header-only date/time library

|

#include "date/date.h"
// ...
using date::operator<<;
os << right.eR[i].duration << " ";

上述datelib现在是C ++ 20的一部分,但尚未发布。供应商正在努力。移植到它时,只需拖放#include "date/date.h"using date::operator<<;

,

我想您正在使用Visual Studio。更新它。

Visual Studio中的

已损坏。它不适用于混合类型算术,而混合类型算术无疑是的主要功能之一。之所以会出现此错误,是因为一侧使用__int64纳米,而另一侧使用double纳米。

我建议要么放弃它以支持真正的C ++实现,要么使用Boost.Chrono。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。