-2

I think there may be something wrong with my gcc and g++ install because this code below will not run on my computer.

#include <iostream>
#include <numbers>

int main()
{
    long double pi {0};
    long double pi2 {0};

    pi  = std::numbers::pi_v<long double>;
    pi2 = std::numbers::pi_v<long double>;

    std::cout << pi << std::endl << pi2;

}

How can I do a full reinstall on gcc and g++? Also, how do I make sure CodeBlocks is using this reinstalled version.

EDIT: running g++ version 11.1.0 & using the -std=c++20 this is the error message that appears:

g++ randomCodeWhileReading.cpp -o -std=c++20
randomCodeWhileReading.cpp: In function ‘int main()’:
randomCodeWhileReading.cpp:9:16: error: ‘std::numbers’ has not been declared
    9 |     pi  = std::numbers::pi_v<long double>;
      |                ^~~~~~~
randomCodeWhileReading.cpp:9:30: error: expected primary-expression before ‘long’
    9 |     pi  = std::numbers::pi_v<long double>;
      |                              ^~~~
randomCodeWhileReading.cpp:10:16: error: ‘std::numbers’ has not been declared
   10 |     pi2 = std::numbers::pi_v<long double>;
      |                ^~~~~~~
randomCodeWhileReading.cpp:10:30: error: expected primary-expression before ‘long’
   10 |     pi2 = std::numbers::pi_v<long double>;
      |                              ^~~~
ExZerminator
  • 65
  • 2
  • 6
  • 1
    What do you mean by the code will not run? Are you getting a compiler error, or is there a runtime error? It's _very_ unlikely that there's something wrong with you GCC installation that reinstalling would fix. Also, `` was only recently introduced in C++20. Does your GCC version support C++20, and are you compiling with `-std=c++20` or `-std=c++2a`? – Brian61354270 Oct 05 '21 at 23:40
  • Can you please give some details about what doesn't work exactly? Compiler versions, standard versioning flags etc. would be helpful also. – πάντα ῥεῖ Oct 05 '21 at 23:41
  • 2
    An example of an [XY problem](https://en.wikipedia.org/wiki/XY_problem): My bedroom window won't open, so I'm going to tear down and rebuild my house; how do I make sure I sleep in the new house? Not exactly the best way to address the problem. – JaMiT Oct 06 '21 at 00:09
  • 1
    Note that it is entirely possible that you will need a new version of GCC on your PC because your distribution shipped with a version that is too old to be of use, but due diligence: Make sure first. If you have to update, try to install it with the apt package manager before you try anything else. – user4581301 Oct 06 '21 at 00:12
  • Does this answer your question? [How to include header file and use std::numbers](https://stackoverflow.com/questions/69442953/how-to-include-numbers-header-file-and-use-stdnumbers) – francesco Oct 08 '21 at 08:26

1 Answers1

1

Please: ALWAYS copy/paste your error message when you post a question!

I suspect this is the compile error you're getting:

x.cpp:2:10: fatal error: numbers: No such file or directory
 #include <numbers>

The header is only available since C++20: https://en.cppreference.com/w/cpp/numeric

Q: Do you have a C++20-compatible version of G+? You can determine this using g++ --version

Q: Are you compiling for C++ 20 (e.g. -std=c++20)?

Here is the Gnu C++ documentation for C++ 20 compatibility:

https://gcc.gnu.org/projects/cxx-status.html

C++20 Support in GCC GCC has experimental support for the latest revision of the C++ standard, which was published in 2020.

C++20 features are available since GCC 8. To enable C++20 support, add the command-line parameter -std=c++20 (use -std=c++2a in GCC 9 and earlier) to your g++ command line. Or, to enable GNU extensions in addition to C++20 features, add -std=gnu++20.

Important: Because the ISO C++20 standard is very recent, GCC's support is experimental.

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 1
    @ExZerminator - Q: so why was your question closed? A: *BECAUSE YOU SHOULD HAVE BEEN SPECIFIC WHAT YOU MEANT BY "something wrong"*! Please consider updating your post ([Edit]). Please 1) Clarify what was "wrong", 2) specify your G++ version. 3) Let us know if your resolved the issue... and if any of my response helped. – paulsm4 Oct 06 '21 at 01:08
  • edited the post to include error and version. – ExZerminator Oct 06 '21 at 18:25
  • The [Libstdc++ manual](https://gcc.gnu.org/onlinedocs/libstdc++/manual/status.html#status.iso.2020) might be another place to look. It mentions "Mathematical constants" (third row from the bottom of the table) as being first implemented in version 10.1 (but it's not clear to me if that is supposed to mean "fully implemented"). – JaMiT Oct 07 '21 at 02:41