涉及输出计时持续时间的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。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...