我遇到的问题与在 a.cpp 中写为 t1_t0 = t1 - t0;
的类似类型之间的操作结果类型的声明有关。 t1
和t0
分别是最终时间和初始时间,其差值用于衡量一次计算之间的时间跨度。我尝试在整个函数中使用 auto
来代替变量 boost::chrono::high_resolution_clock::time_point
、std::chrono::seconds
和 int
,但是在声明变量之前调用函数之后,这会导致奇怪的问题。
它被称为 error: use of before deduction of ‘auto’
编译错误,所以我尝试使用真实类型编译项目,但现在我遇到了这个麻烦。如果类在 int main
上方声明,该项目确实可以正常编译(使用 auto
),但如果该类被分为头文件和 cpp 文件,则该项目无法正常工作.
这是整个项目,请帮忙!
#include <iostream>
#include "a.h"
int main()
{
a A;
int timer = A.time();
std::cout<<timer<<std::endl;
return 0;
}
这是啊
#ifndef A_H
#define A_H
#include <iostream>
#include <boost/chrono/chrono_io.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/thread/thread.hpp>
#include <sstream>
#include <cassert>
#include <chrono>
class a
{
private:
public:
a();
~a();
int time();
};
#endif
这是a.cpp
#include "a.h"
a::a(){}
a::~a(){}
int a::time()//simple benchmark timer
{
boost::chrono::high_resolution_clock::time_point t0, t1, t1_t0;
t0 = boost::chrono::high_resolution_clock::now();
//Run a process to measure time.
t1 = boost::chrono::high_resolution_clock::now();
t1_t0 = t1 - t0;//Not sure what the resulting type is here?
std::chrono::seconds nsec = std::chrono::duration_cast<std::chrono::seconds>(t1_t0);
//Not sure what the resulting type is here?
return nsec.count();//Not sure what the return type is here?
}
这是编译它的错误。
||=== Build: Debug in return_type_auto_from_class (compiler: GNU GCC Compiler) ===|
/home/Desktop/a.cpp||In member function ‘int a::time()’:|
/home/Desktop/a.cpp|14|error: no match for ‘operator=’ (operand types are ‘boost::chrono::steady_clock::time_point {aka boost::chrono::time_point<boost::chrono::steady_clock>}’ and ‘boost::common_type<boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> >, boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> > >::type {aka boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> >}’)|
/usr/include/boost/chrono/time_point.hpp|156|note: candidate: constexpr boost::chrono::time_point<boost::chrono::steady_clock>& boost::chrono::time_point<boost::chrono::steady_clock>::operator=(const boost::chrono::time_point<boost::chrono::steady_clock>&)|
/usr/include/boost/chrono/time_point.hpp|156|note: no known conversion for argument 1 from ‘boost::common_type<boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> >, boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> > >::type {aka boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> >}’ to ‘const boost::chrono::time_point<boost::chrono::steady_clock>&’|
/usr/include/boost/chrono/time_point.hpp|156|note: candidate: constexpr boost::chrono::time_point<boost::chrono::steady_clock>& boost::chrono::time_point<boost::chrono::steady_clock>::operator=(boost::chrono::time_point<boost::chrono::steady_clock>&&)|
/usr/include/boost/chrono/time_point.hpp|156|note: no known conversion for argument 1 from ‘boost::common_type<boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> >, boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> > >::type {aka boost::chrono::duration<long int, boost::ratio<1l, 1000000000l> >}’ to ‘boost::chrono::time_point<boost::chrono::steady_clock>&&’|
/home/Desktop/a.cpp|16|error: no matching function for call to ‘duration_cast(boost::chrono::steady_clock::time_point&)’|
/usr/include/c++/5/chrono|194|note: candidate: template<class _ToDur, class _Rep, class _Period> constexpr typename std::enable_if<std::chrono::__is_duration<_Tp>::value, _ToDur>::type std::chrono::duration_cast(const std::chrono::duration<_Rep, _Period>&)|
/usr/include/c++/5/chrono|194|note: template argument deduction/substitution failed:|
/home/Desktop/a.cpp|16|note: ‘boost::chrono::steady_clock::time_point {aka boost::chrono::time_point<boost::chrono::steady_clock>}’ is not derived from ‘const std::chrono::duration<_Rep, _Period>’|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|
编辑过的代码,这确实修复了原始代码,现在可以使用了。
int a::time()
{
boost::chrono::high_resolution_clock::time_point t0, t1;//, t1_t0;
t0 = boost::chrono::high_resolution_clock::now();
//Run a process to measure time.
boost::this_thread::sleep_for( boost::chrono::milliseconds{ 2000});
t1 = boost::chrono::high_resolution_clock::now();
auto t1_t0 = t1 - t0;
auto nsec = boost::chrono::duration_cast<boost::chrono::seconds>(t1_t0);
return nsec.count();
}
最佳答案
不要混合使用
boost::chrono
和std::chrono
。使用一个或另一个。如果您的平台提供并支持std::chrono
,我推荐它,否则使用boost::chrono
。两个
time_point
之间的区别是duration
,而不是另一个time_point
。例如,“明天”和“今天”可以被认为是时间点(精度非常低)。明天 - 今天 == 1 天
。一天是一段持续时间。如果你想要纳秒级的最终结果,没必要这么努力:
纳秒 nsec = t1 - t0;
如何从
a::time()
返回nanoseconds
而不是int
?这样,客户端就有了一个类型安全的持续时间,而不是一个可能意味着任何东西的int
。返回 t1 - t0;
关于c++ - 将 boost::chrono 计时机制包装到类编译错误中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44319322/