C++ Constexpr 未在此范围内声明

标签 c++ compiler-errors constexpr

我正在为 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/

相关文章:

c++ - constexpr with operator|=

C++:尝试通过组合和帕斯卡三角来理解 constexpr

java - Sun JRE javac 和 Eclipse java 编译器之间的不一致?

c++ - 为什么要使用 umask?

c++ - 在某些情况下,std::is_floating_point 为 float 返回 false

c++ - 如何使可变 lambda 捕获列表的某些成员成为 const?

c++ - 为模板类编写二元加法运算符。编译错误

c++ - 在 Ubuntu 14.04 上使用 icpc 编译 Qt 时出现编译错误

c++ - 如何从 `std::integer_sequence` 创建 `__func__` ?

c++ - 打印作业时,最后的作业状态是 JOB_STATUS_RETAINED,而不是 JOB_STATUS_PRINTED