c++gccg++codeblocksc++20

How to include <numbers> header file and use std::numbers


running on version 11.1.0 of gcc and g++. Every time I run this code I run into issues it says std::numbers was not declared. I tried running g++ randomCodeWhileReading.cpp -o main-std=c++20 within my terminal (im running ubuntu linux) and still no change. Here is the code in question:

#include <iostream>
#include <numbers>
int main()
{
    const long double pi {0};
    const long double pi2 {0};

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

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

}

Just wanted to see the numbers module in action nothing else. (is it even called a module or is it a header file?)

EDIT 10/6/21: The modifying a constant variable has been fixed. However, this code still wont run on my computer. Namely, the #include <numbers> does not seem to work on my machine it throws an error even when using -std=c++20. I am running gcc and g++ version 11.1 See error below:

gcc ex2_03.cpp -o -std=c++20
ex2_03.cpp: In function ‘int main()’:
ex2_03.cpp:22:65: error: ‘std::numbers’ has not been declared
   22 |     const double pond_diameter {2.0 * std::sqrt(pond_area/ std::numbers::pi)}; //find diameter by finding radius & multiplying by 2
      |    

however I was unable to replicate using godbolt.org (similar program not the same but uses as well). Clearly, it seems that this is an issue with my machine. How would I go about fixing this?

EDIT 10/8/21: I ran the code again using more flags and changing -std=c++20 to -std=c++2a this was what was returned:

chris@chris-Aspire-E5-576G:~/Desktop/programming/c++/Learning$ ls
ex2_02      HelloWorld          randomCodeWhileReading      textbookExample1
ex2_02.cpp  HelloWorld.cpp      randomCodeWhileReading.cpp  textbookExample1.cpp
ex2_02.o    HelloWorld.o        randomCodeWhileReading.o    textbookExample1.o
ex2_03      main                textbookDebug               textbookOutputNameAndAge.cpp
ex2_03.cpp  outputNameAndAge    textbookDebug.cpp
ex2_03.o    outputNameAndAge.o  textbookDebug.o
chris@chris-Aspire-E5-576G:~/Desktop/programming/c++/Learning$ g++ -g -Wall -pedantic -std=c++2a -o randomCodeWhileReading.cpp
g++: fatal error: no input files
compilation terminated.

added the ls output to show I was in the correct directory.

EDIT 10/8/21 v2: I used the following command and did not receive an error.

g++ randomCodeWhileReading.cpp -o main -std=c++20

Now just confused where the output went. By @nate's responses I assume it was sent to main? Just wanted to see a cout using std::numbers::pi

EDIT 10/8/21 v3: All clear nate explained program can be ran by using ./main

EDIT 10/8/21 v4: ... I repeated the earlier command and got a error:

g++ randomCodeWhileReading.cpp -o main -std=c++20
cc1plus: fatal error: randomCodeWhileReading.cpp: No such file or directory
compilation terminated.

can someone explain what went wrong this time? (I am still in the same directory). After using ls it seems that the file is no longer in the directory seems to be deleted?

EDIT 10/8/21 v5: I think the file got deleted when I was explaining the error to a friend and the wrong ways I was running the command lol. All good :D !


Solution

  • You need to compile with the extra flag -std=c++20.

    Moreover, there is an error in your code: pi and pi2 are declared const, hence you cannot modify them after they are initialized. Use this instead:

    #include <iostream>
    #include <numbers>
    
    int main()
    {
        const long double pi = std::numbers::pi_v<long double>;
        const long double pi2 = std::numbers::pi_v<long double>;
        std::cout << pi << std::endl << pi2;
    }