在 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;
}
只是想看看数字模块的运行情况,没有别的。 (它甚至被称为模块还是头文件?)
编辑 21 年 10 月 6 日:
修改常量变量已修复。但是,这段代码仍然无法在我的计算机上运行。即,#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
|
但是我无法使用复制上帝螺栓.org https://godbolt.org/z/zvMPv9ooP(类似的程序不一样但也使用)。显然,这似乎是我的机器的问题。我该如何解决这个问题?
编辑 21 年 10 月 8 日:
我使用更多标志再次运行代码并将 -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?只是想看看 cout 使用std::numbers::pi
编辑 10/8/21 v3:
所有清晰的 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:
我认为当我向朋友解释错误以及我运行命令的错误方式时,该文件被删除了,哈哈。一切都好:D!