2014-07-05 113 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) –