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

john picture john · Aug 6, 2014 · Viewed 15.7k times · Source

I am running my below code which checks whether data_timestamp is more than two weeks old or not. If it is two weeks old, then print hello otherwise prints world.

I am a Java developer, recently started working with C++. Learned few things over internet so I am using it here in this program.

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

I will be running this code on Ubuntu 12.04. When I am compiling it while running make install, it is giving me this exception -

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

May be, I am not having C++11. This is a simple program which I made but the core logic of this program I am using it in a big C++ project so it looks like, I cannot port everything to C++11 to make this work. Is there any other way by which I can write this code which does not use C++11?

Update:-

This is the way I am getting current timestamp in milliseconds in that big project at some part of the code -

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

Answer

Brian Bi picture Brian Bi · Aug 6, 2014

The new meaning of auto (deduce the type) was introduced in C++11. Compile your code with the flag -std=c++11.