Failed to use srand48 () after switching to C ++ 11

Why can't I compile my code in C ++ 11 and use the srand48 function?

I have a program in which I play with some matrices. The problem is that when I compile the code with the flag -std=c++0x . I want to use some only C ++ 11 features, and this is my approach to do this. It compiles without any problems unless I specify a C ++ version. Like this:

 g++ -O2 -Wall test.cpp -o test -g 

Please correct me if I misunderstood what the flag mentioned does.

I run my code on a 64-bit Windows 7 machine and compile through cygwin. I am using g ++ version 4.5.3 (GCC). Please comment if additional information is required.

For some unknown reason (even for myself), all my code is written in one compilation unit. If the error is caused by a structural error, you can also not indicate it. :)

I get the following errors :

 g++ -std=c++0x -O2 -Wall test.cpp -o test -g test.cpp: In function 'void gen_mat(T*, size_t)': test.cpp:28:16: error: there are no arguments to 'srand48' that depend on a template parameter, so a declaration of 'srand48' must be available test.cpp:28:16: note: (if you use '-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated) test.cpp:33:28: error: there are no arguments to 'drand48' that depend on a template parameter, so a declaration of 'drand48' must be available 

Here is a subcomplex of my code , it generates the errors shown above.

 #include <iostream> #include <cstdlib> #include <cassert> #include <cstring> #include <limits.h> #include <math.h> #define RANGE(S) (S) // Precision for checking identity. #define PRECISION 1e-10 using namespace std; template <typename T> void gen_mat(T *a, size_t dim) { srand48(dim); for(size_t i = 0; i < dim; ++i) { for(size_t j = 0; j < dim; ++j) { T z = (drand48() - 0.5)*RANGE(dim); a[i*dim+j] = (z < 10*PRECISION && z > -10*PRECISION) ? 0.0 : z; } } } int main(int argc, char *argv[]) { } 

Regards, Kim.

This is the solution that solved the problem for me:

First N.M. explained that srand () cannot be used when compiling with -std=c++0x . The correct flag to use is -std=gnu++11 , but it requires g ++ version 4.7+, so the solution for me was to compile my code with -std=gnu++0x Compilation command = g++ -O2 -Wall test.cpp -o test -g -std=gnu++0x

+4
source share
1 answer

If you explicitly set -stc=c++03 , you will get the same error. This is because drand48 and friends are not really part of any C ++ standard. gcc enables these functions as an extension and disables them if standard behavior is requested.

The default g++ default mode is actually -std=gnu++03 . You can use -std=gnu++11 instead of -std=c++0x or pass -U__STRICT_ANSI__ compiler.

+5
source

Source: https://habr.com/ru/post/1481107/


All Articles