这是我正在使用的类(class)(某些部分被剪掉了)
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
没有流式运算符类型。你有两个选择:
.count()
: |
os << right.eR[i].duration.count() << "s ";
|
#include "date/date.h"
// ...
using date::operator<<;
os << right.eR[i].duration << " ";
上面的 datelib 现在是 C++20 的一部分,但尚未发布。供应商正在为此努力。当您移植到它时,您只需删除 #include "date/date.h"
和 using date::operator<<;
.
关于涉及输出计时持续时间的 C++ 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64181934/