我正在为 Udemy.com 上的其中一门类(class)开发一个项目,但一直收到此错误;
/home/scott/bullcowgame/src/main.cc:40:3: 错误:“constexpr”未在此范围内声明 /home/scott/bullcowgame/src/main.cc:40:13: error: expected ‘;’ before ‘int’ /home/scott/bullcowgame/src/main.cc:42:35: 错误:“WORD_LENGTH”未在此范围内声明
这是我的代码
#include <iostream>
#include <string>
using namespace std;
void PrintIntro();
string GetGuessAndPrintBack();
// the entry point for our application
int main()
{
PrintIntro();
GetGuessAndPrintBack();
GetGuessAndPrintBack();
cout << endl;
return 0;
}
// introduce the game
void PrintIntro() {
constexpr int WORD_LENGTH = 9;
cout << "Welcome to Bulls and Cows, a fun word game.\n";
cout << "Can you guess the " << WORD_LENGTH;
cout << " letter isogram I'm thinking of?\n";
cout << endl;
return;
}
// get a guess from the player
string GetGuessAndPrintBack() {
cout << "Enter your guess: ";
string Guess = "";
getline(cin, Guess);
// print the guess back
cout << "Your guess was: " << Guess << endl;
return Guess;
}
我正在使用 Anjuta IDE
最佳答案
您的编译器似乎不支持 constexpr
。您应该检查您的编译器是否使用标志支持它(即用于编译的默认标准早于 C++11)。
否则你将不得不下载一个支持它的(或者放弃使用 constexpr)。 如果您不在 Windows 上,GCC 将支持它。对于 Windows,我想 Cygwin/Mingwin 支持它,但我不确定。 Clang 应该在所有平台上都支持它。
关于C++ Constexpr 未在此范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44103085/