1

Looking for code to implement numpy's arange function in c++, I found this answer.

I placed the following code in a file test_arange_c.cpp:

#include <vector>

template<typename T>
std::vector<T> arange(T start, T stop, T step = 1)
{
  std::vector<T> values;
  for (T value = start; value < stop; value += step)
    values.push_back(value);
  return values;
}

int main()
{
  double dt;
  dt = 0.5;
  auto t_array = arange<double>(0, 40, dt);
  return 0;
}

When I try to compile it, I get the following error:

$ c++ test_arange_c.cpp -o test_arange_c.out
test_arange_c.cpp: In function ‘int main()’:
test_arange_c.cpp:14:8: error: ‘t_array’ does not name a type
   auto t_array = arange<double>(0, 40, dt);

Without doubt, I've made a mistake that will be obvious to seasoned c++ users. But, after searching Google for a while, I haven't come up with what it is.

Community
  • 1
  • 1
abcd
  • 10,215
  • 15
  • 51
  • 85
  • 1
    It looks like you haven't enabled C++11 support, or your compiler doesn't have it. – Brian Bi Oct 13 '15 at 00:07
  • @Brian is there an easy way to check for this? would reporting my compiler version tell you whether that's the case? – abcd Oct 13 '15 at 00:08
  • You can check whether C++11 is enabled for a given compilation using the `__cplusplus` macro. http://stackoverflow.com/questions/5047971/how-do-i-check-for-c11-support – Brian Bi Oct 13 '15 at 00:15
  • If you want to know whether your compiler implements C++11 at all, you should consult the documentation. – Brian Bi Oct 13 '15 at 00:15
  • @Brian what keyword is it exactly that requires C++11 here? (i want to change the question title so people who get this error with this keyword in the future have an easier time finding the solution.) – abcd Oct 13 '15 at 01:46
  • You need C++11 for your use of `auto`. Before C++11, `auto` meant "this is an automatic variable" and would have to be followed by a type. – Brian Bi Oct 13 '15 at 05:39

1 Answers1

1

As @Brian suggested, I had not enabled C++11 support.

$ c++ --version
c++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

This fails:

$ c++ test_arange_c.cpp -o test_arange_c.out
test_arange_c.cpp: In function ‘int main()’:
test_arange_c.cpp:16:8: error: ‘t_array’ does not name a type
   auto t_array = arange<double>(0, 40, dt);
        ^

This works:

$ c++ -std=c++11 test_arange_c.cpp -o test_arange_c.out
$
abcd
  • 10,215
  • 15
  • 51
  • 85