#include <iostream>
using namespace std;
int main()
{
constexpr double GetPi() {return 22.0/7;}
cout << GetPi() << endl;
}
不知道这里发生了什么。请帮我。 我试图编译它,甚至尝试重新格式化代码,但它并没有真正帮助太多。我认为这是你做事的正确方法吗?这里发生了什么?这是一个不变的表达。
以下是编译错误:
C:\Windows\system32\cmd.exe /C "C:/TDM-GCC-64/bin/mingw32-make.exe -j4` SHELL=cmd.exe -e -f Makefile"
"----------Building project:[ Constant_Expression - Debug ]----------"
mingw32-make.exe[1]: Entering directory 'C:/Program Files/CodeLite/CPP/Constants/Constant_Expression'
codelite-cc C:/TDM-GCC-64/bin/g++.exe -c "C:/Program Files/CodeLite/CPP/Constants/Constant_Expression/main.cpp" -g -O0 -std=c++11 -Wall -o ./Debug/main.cpp.o -I. -I.
C:/Program Files/CodeLite/CPP/Constants/Constant_Expression/main.cpp: In function 'int main()':
C:/Program Files/CodeLite/CPP/Constants/Constant_Expression/main.cpp:7:30: error: a function-definition is not allowed here before '{' token
constexpr double GetPi() {return 22.0/7;}
^
C:/Program Files/CodeLite/CPP/Constants/Constant_Expression/main.cpp:8:19: error: 'GetPi' was not declared in this scope
cout << GetPi() << endl;
^
mingw32-make.exe[1]: *** [Debug/main.cpp.o] Error 1
Constant_Expression.mk:93: recipe for target 'Debug/main.cpp.o' failed
mingw32-make.exe[1]: Leaving directory 'C:/Program Files/CodeLite/CPP/Constants/Constant_Expression'
mingw32-make.exe: *** [All] Error 2
Makefile:4: recipe for target 'All' failed
3 errors, 0 warnings
答案 0 :(得分:0)
在C ++中,不允许在另一个函数中定义函数。 (不像javascript中的闭包)。 您必须在main()函数
之外定义GetPI()#include <iostream>
using namespace std;
constexpr double GetPi() {return 22.0/7;}
int main()
{
cout << GetPi() << endl;
}