如何包含<Numbers&>头文件并使用std::Numbers [英] How to include <numbers> header file and use std::numbers

查看:29
本文介绍了如何包含<Numbers&>头文件并使用std::Numbers的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在GCC和g++11.1.0版本上运行。每次我运行这段代码时,我都会遇到一些问题,它显示未声明std::Numbers。我尝试在我的终端中运行g++ randomCodeWhileReading.cpp -o main-std=c++20(我正在运行ubuntu Linux),仍然没有变化。以下是有问题的代码:

#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;

}

只是想看看Numbers模块的运行情况,没有别的。(它是称为模块还是头文件?)

编辑10/6/21: 已修复修改常量变量的问题。然而,这个代码仍然不能在我的电脑上运行。也就是说,#include <numbers>似乎在我的机器上不起作用,即使使用-std=c++20也会抛出错误。我正在运行GCC和g++11.1版请参见下面的错误:

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
      |    

然而,我无法使用godbolt.org复制(类似的程序不同,但也使用)。显然,这似乎是我的机器的问题。我该如何着手解决这个问题?

编辑10/8/21: 我使用更多标志再次运行代码,并将-std=c++20更改为-std=c++2a,返回的内容如下:

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.

添加ls输出以显示我位于正确的目录中。

编辑10/8/21 v2: 我使用了以下命令,但没有收到错误。

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

现在只是混淆了输出到哪里去了。根据@Nate的回复,我猜它是发送到Main的?只想使用std::numbers::pi

查看COUT

编辑10/8/21 v3: 所有Clear Nate解释程序都可以使用./main

运行

编辑10/8/21 v4: ..。我重复了前面的命令,但收到错误:

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

谁能解释一下这次出了什么问题?(我仍然在同一个目录中)。使用ls后,该文件似乎已不在目录中,似乎已被删除?

编辑10/8/21 v5: 我想当我向一个朋友解释这个错误时,文件被删除了,而且我运行命令LOL的方式也是错误的。一切正常:d!

推荐答案

您需要使用额外的标志-std=c++20进行编译。

此外,您的代码中有一个错误:pipi2被声明为const,因此在初始化后无法修改它们。改用此选项:

#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;
}

这篇关于如何包含&lt;Numbers&>头文件并使用std::Numbers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆