2017-06-02 64 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