2017-06-02 47 views
1

我遇到的问题与在a.cpp中编写为t1_t0 = t1 - t0;的类似类型之间的操作结果类型的声明有关。 t1t0分别是最终和最初的时间,差异用于测量一次计算之间的时间跨度。我试着用auto代替变量boost::chrono::high_resolution_clock::time_point,std::chrono::secondsint,但是在函数被调用之前,声明了变量后引起了奇怪的问题。将boost :: chrono计时机制封装到类编译错误中?

它被称为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; 
} 

这是A.H

#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(); 
} 

回答

2
  1. 请勿混用boost::chronostd::chrono。使用一个或另一个。如果您的平台提供并支持,我推荐std::chrono,否则请使用boost::chrono

  2. 两个time_point s之间的差异是duration,而不是另一个time_point。例如,“明天”和“今天”可以被认为是时间点(具有非常粗略的精度)。 tomorrow - today == 1 day。一天是一个持续时间。

  3. 如果你想最终结果为纳秒,没必要着急:

    nanoseconds nsec = t1 - t0;

  4. 如何返回nanoseconds,而不是从inta::time()?这样客户有一个类型安全的持续时间,而不是一个int这可能意味着什么。

    return t1 - t0;

+0

1.帮我编译它! 2.虽然持续时间是什么样的提升类型? – pandoragami

+0

Nevermind'nanoseconds'是一个boost中的变量类型吗?我会改变它。 – pandoragami

+0

我试着将返回类型从'int'更改为'nanoseconds',并且没有奏效。如果你可以发布你的代码,那很好。上面的修补程序的代码进行编译,它的工作原理! – pandoragami