Results 1 to 8 of 8

Thread: GNU compiler cant load a new c++ header file

  1. #1
    Join Date
    Apr 2013
    Beans
    3

    GNU compiler cant load a new c++ header file

    hi there,

    im trying to using a new header file <random> from C++ Standard Library. I simply copy-paste the following example on http://www.cplusplus.com/reference/r..._distribution/


    Code:
    // bernoulli_distribution 
    #include <iostream> 
    #include <random> 
     int main() {  
     const int nrolls=10000;   
     std::default_random_engine generator;   
    std::bernoulli_distribution distribution(0.5);   
     int count=0;  // count number of trues   
     for (int i=0; i<nrolls; ++i) if (distribution(generator)) ++count;   
     std::cout << "bernoulli_distribution (0.5) x 10000:" << std::endl;   
    std::cout << "true:  " << count << std::endl;  
     std::cout << "false: " << nrolls-count << std::endl;   
    return 0; 
    }
    Then I got the following errors after execution of the file "test.cpp" containing the code above:
    Code:
    g++ test.cpp
    In file included from /usr/include/c++/4.4/random:35,
                     from test.c:3:
    /usr/include/c++/4.4/c++0x_warning.h:31:2: error: #error This file requires compiler and library support for the upcoming ISO C++ standard, C++0x. 
    This support is currently experimental, and must be enabled with the -std=c++0x or -std=gnu++0x compiler options.
    test.cpp: In function ‘int main()’:
    test.cpp:9: error: ‘default_random_engine’ is not a member of ‘std’
    test.cpp:9: error: expected ‘;’ before ‘generator’
    test.cpp:10: error: ‘bernoulli_distribution’ is not a member of ‘std’
    test.cpp:10: error: expected ‘;’ before ‘distribution’
    test.cpp:14: error: ‘generator’ was not declared in this scope
    test.cpp:14: error: ‘distribution’ was not declared in this scope
    I think the message on line 4 "...and must be enabled with the -std=c++0x or -std=gnu++0x compiler options." is important.

    I´ve tried
    Code:
    sudo apt-get install linux-headers-`uname -r`
    But that doesnt help. Apart from this I have to admit at this point that I have no idea what else to do here.

    Anyone can tell me how to deal with it? Some command codes would be very nice

    thank you very much!

    best regards

  2. #2
    Join Date
    Apr 2012
    Beans
    7,093

    Re: GNU compiler cant load a new c++ header file

    Have you tried doing exactly what it told you to do? i.e. changing your g++ command line to

    Code:
    g++ -std=c++0x test.cpp

  3. #3
    Join Date
    Apr 2006
    Location
    Ubuntuland
    Beans
    2,124
    Distro
    Ubuntu 13.10 Saucy Salamander

    Lightbulb Re: GNU compiler cant load a new c++ header file

    Quote Originally Posted by gravit123 View Post
    <cut>
    I think the message on line 4 "...and must be enabled with the -std=c++0x or -std=gnu++0x compiler options." is important.
    <cut>
    Indeed! Give it a try.
    24 beers in a case, 24 hours in a day. Coincidence? I think not!

    Trusty Tahr 64 bit, AMD Phenom II 955 Quad Core 3.2GHz, GeForce 9600 GT
    16G PC2-6400 RAM, 128 GB SSD, Twin 1TB SATA 7200 RPM RAID0

  4. #4
    Join Date
    Apr 2013
    Beans
    3

    Re: GNU compiler cant load a new c++ header file

    I now have a shorter error code

    Code:
    g++ -std=c++0x test.cpp
    test.cpp: In function ‘int main()’:
    test.cpp:9: error: ‘default_random_engine’ is not a member of ‘std’
    test.cpp:9: error: expected ‘;’ before ‘generator’
    test.cpp:14: error: ‘generator’ was not declared in this scope
    looks like gnu still dont recognize std::macros from <random>. Using -std=gnu++0x returns the same error.
    Last edited by gravit123; April 19th, 2013 at 03:29 PM.

  5. #5
    Join Date
    Apr 2012
    Beans
    7,093

    Re: GNU compiler cant load a new c++ header file

    Well I copy-pasted exactly the fragment you posted above and it works for me on 12.04 / g++ 4.6.3

    Code:
    $ cat > test.cpp <<EOF
    > // bernoulli_distribution
    > #include <iostream>
    > #include <random>
    >  int main() {
    >  const int nrolls=10000;
    >  std::default_random_engine generator;
    > std::bernoulli_distribution distribution(0.5);
    >  int count=0;  // count number of trues
    >  for (int i=0; i<nrolls; ++i) if (distribution(generator)) ++count;
    >  std::cout << "bernoulli_distribution (0.5) x 10000:" << std::endl;
    > std::cout << "true:  " << count << std::endl;
    >  std::cout << "false: " << nrolls-count << std::endl;
    > return 0;
    > }
    > EOF
    $
    $ g++ -std=c++0x test.cpp
    $
    $ ./a.out
    bernoulli_distribution (0.5) x 10000:
    true:  4994
    false: 5006
    What g++ version are you using?

  6. #6
    Join Date
    May 2010
    Location
    uk
    Beans
    9,324
    Distro
    Xubuntu 14.04 Trusty Tahr

    Re: GNU compiler cant load a new c++ header file

    Hi

    Same as steeldriver. No errors.
    Code:
    matthew-S206:/home/matthew % cat > test.cpp <<"EOF"
    heredoc> // bernoulli_distribution 
    heredoc> #include <iostream> 
    heredoc> #include <random> 
    heredoc>  int main() {  
    heredoc>  const int nrolls=10000;   
    heredoc>  std::default_random_engine generator;   
    heredoc> std::bernoulli_distribution distribution(0.5);   
    heredoc>  int count=0;  // count number of trues   
    heredoc>  for (int i=0; i<nrolls; ++i) if (distribution(generator)) ++count;   
    heredoc>  std::cout << "bernoulli_distribution (0.5) x 10000:" << std::endl;   
    heredoc> std::cout << "true:  " << count << std::endl;  
    heredoc>  std::cout << "false: " << nrolls-count << std::endl;   
    heredoc> return 0; 
    heredoc> }
    heredoc> EOF
    matthew-S206:/home/matthew % g++ -std=c++0x test.cpp
    matthew-S206:/home/matthew % ./a.out 
    bernoulli_distribution (0.5) x 10000:
    true:  4994
    false: 5006
    matthew-S206:/home/matthew %
    Code:
    matthew-S206:/home/matthew % g++ --version
    g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
    Just thought i would pile in with this rather useless post

    Kind regards
    If you believe everything you read, you better not read. ~ Japanese Proverb

    If you don't read the newspaper, you're uninformed. If you read the newspaper, you're mis-informed. - Mark Twain

    Thinking about becoming an Ubuntu Member?

  7. #7
    Join Date
    Apr 2013
    Beans
    3

    Re: GNU compiler cant load a new c++ header file

    oh my! I have an old gcc on old ubuntu.I´ll update it

    Code:
    g++ -v
    Using built-in specs.
    Target: i486-linux-gnu
    Configured with: ../src/configure -v --with-pkgversion='Ubuntu 4.4.3-4ubuntu5.1' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --program-suffix=-4.4 --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-plugin --enable-objc-gc --enable-targets=all --disable-werror --with-arch-32=i486 --with-tune=generic --enable-checking=release --build=i486-linux-gnu --host=i486-linux-gnu --target=i486-linux-gnu
    Thread model: posix
    gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5.1)
    thank you for all your anwers

    cheers

  8. #8
    Join Date
    Apr 2012
    Beans
    7,093

    Re: GNU compiler cant load a new c++ header file

    ... a bit of googling suggests you could *try*

    Code:
    g++ -D__GXX_EXPERIMENTAL_CXX0X__ -std=c++0x test.cpp
    if updating is difficult (which it sometimes is)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •