2014-07-05 111 views
3

我正在將代碼從Linux移植到Window。C++ 11 chrono in visual studio 2013

而且我沒有想到在使用std :: chrono時會出現一個錯誤。

因爲std :: chrono是C++標準庫,我預計它在未經修改的情況下工作。

下面是顯示錯誤的代碼。

錯誤發生在我使用運算符的持續時間實例和duration_cast函數with no instance of function template的部分。

在Linux中

,代碼工作正常

std::string ChronoTimer::currentTime(){ 
    using namespace std::chrono; 
    auto now = system_clock::now(); 
    time_point<system_clock> epoch; 

    microseconds ms = duration_cast<milliseconds>(now - epoch); 

    hours hour = duration_cast<hours>((ms % hours(24)) + hours(9)); 
    minutes min = duration_cast<minutes>(ms % hours(1)); 
    seconds sec = duration_cast<seconds>(ms % minutes(1)); 
    milliseconds msec = duration_cast<milliseconds>(ms % seconds(1)); 


    std::stringstream strStream; 
    strStream << std::setfill('0') << std::setw(2) << hour.count() << ":"; 
    strStream << std::setfill('0') << std::setw(2) << min.count() << ":"; 
    strStream << std::setfill('0') << std::setw(2) << sec.count() << "."; 
    strStream << std::setfill('0') << std::setw(3)<< msec.count(); 
    return strStream.str(); 
} 

enter image description here

1 IntelliSense: no instance of function template "std::chrono::duration_cast" matches the argument list 
     argument types are: (<error-type>) 


2 IntelliSense: no operator "+" matches these operands 
     operand types are: std::chrono::system_clock::rep + std::chrono::hours 

3 IntelliSense: no instance of function template "std::chrono::duration_cast" matches the argument list 
     argument types are: (std::chrono::system_clock::rep)  
+2

如何向我們展示**實際的錯誤信息**? (並且在你發佈的代碼中清楚地標明它出現的位置) –

+0

你得到的錯誤是什麼? – Zacrath

+0

對不起,我添加內容 – SangminKim

回答

3

下面我發佈一個SSCCE,專注於您的問題:

#include <chrono> 

using namespace std::chrono; 

int main() { 
    auto now = system_clock::now(); 
    time_point<system_clock> epoch; 
    microseconds ms = duration_cast<milliseconds>(now - epoch); 
    microseconds hs = std::chrono::hours(1); 
    auto mm = ms % hs; 
} 

雖然上面的例子適用於GCCv4.9CLANGv3.4。它無法在VS2013中編譯。

錯誤報告稱VC++無法將std::chrono::microseconds轉換爲std::chrono::system_clock::rep

看來,實施者正在搞亂轉換的東西,我認爲這是一個視覺C++錯誤,應該是reported

+1

[此錯誤](https://connect.microsoft.com/VisualStudio/feedbackdetail/view/959253)已解決,並位於[VS2013 CTP版本14.](http://blogs.msdn.com/b /vcblog/archive/2014/06/03/visual-studio-14-ctp.aspx) –