繁体   English   中英

自动更改意义在C ++ 11中; 请删除它是什么意思?

[英]auto changes meaning in C++11; please remove it what does this mean?

我正在运行我的下面的代码,它检查data_timestamp是否超过两周。 如果它是两周大,那么打印你好否则打印世界。

我是一名Java开发人员,最近开始使用C ++。 通过互联网学到了一些东西,所以我在这个程序中使用它。

#include <ctime>
#include <chrono>
#include <iostream>

int main()
{
    // this has to be uint64_t bcoz of old code
    uint64_t data_timestamp = 1406066507000; 

    const auto now = std::chrono::system_clock::now();
    auto twoWeeks = std::chrono::hours(24 * 14);
    auto lastTwoWeeks = now - twoWeeks;

    auto millis = std::chrono::duration_cast<std::chrono::milliseconds>(lastTwoWeeks.time_since_epoch()).count();

    std::cout << "Time stamp in milliseconds since UNIX epoch start: "<< millis << std::endl;

    if (data_timestamp < millis) { 
        std::cout << "Hello"; 
    } else { 
        std::cout << "World"; 
    }

    return 0;
}

我将在Ubuntu 12.04上运行此代码。 当我在运行make install时编译它时,它给了我这个例外 -

warning: âautoâ changes meaning in C++11; please remove it [-Wc++0x-compat]
error: ânowâ does not name a type
warning: âautoâ changes meaning in C++11; please remove it [-Wc++0x-compat]
âtwoWeeksâ does not name a type
warning: âautoâ changes meaning in C++11; please remove it [-Wc++0x-compat]
error: âlastTwoWeeksâ does not name a type
warning: âautoâ changes meaning in C++11; please remove it [-Wc++0x-compat]
error: âmillisâ does not name a type
error: âmillisâ was not declared in this scope

可能是,我没有C++11 这是我制作的一个简单的程序,但是我在大型C ++项目中使用它的程序的核心逻辑,所以看起来,我无法将所有内容移植到C ++ 11中以使其工作。 有没有其他方法可以编写不使用C ++ 11的代码?

更新: -

这是我在代码的某个部分的大项目中以毫秒为单位获取当前时间戳的方式 -

struct timeval tp;
gettimeofday(&tp, NULL);
uint64_t current_ms = tp.tv_sec * 1000 + tp.tv_usec / 1000; //get current timestamp in milliseconds

auto (推断类型)的新含义是在C ++ 11中引入的。 使用标志-std=c++11编译代码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM