Scott Young
Scott Young

Reputation: 1

C++ Constexpr not declared in this scope

I am working on a project for one of the classes on Udemy.com and keep getting this error;

/home/scott/bullcowgame/src/main.cc:40:3: error: ‘constexpr’ was not declared in this scope /home/scott/bullcowgame/src/main.cc:40:13: error: expected ‘;’ before ‘int’ /home/scott/bullcowgame/src/main.cc:42:35: error: ‘WORD_LENGTH’ was not declared in this scope

this is my code

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

I am using the Anjuta IDE

Upvotes: 0

Views: 3848

Answers (2)

sandeep
sandeep

Reputation: 21

If you are using code blocks go to settings > compiler > compiler settings and check the c++11 option It works for me https://i.sstatic.net/qIYtB.jpg

Upvotes: 0

lesurp
lesurp

Reputation: 339

It seems your compiler does not support constexpr. You should check if your compiler supports it with a flag (ie the default standard used for compilation is older than C++11).

Otherwise you will have to download one that supports it (or give up using constexpr). GCC will support it if you're not on Windows. For Windows, I guess Cygwin/Mingwin support it but I don't know for sure. Clang should support it on all platforms.

Upvotes: 1

Related Questions