switch, constant expression

I have declarated this:
const int peasant = 300;
const int footman = 900;
const int archer = 500;
const int griffon = 1500;
const int hero = 10000;
const int space = 0;
int Field[10][12];

And now my array Field[][] is filled with the declarated consts and I have a function to print is but in more appropriate view:
void Battlefield::PrintField()
{
string piece;
for (int i = 0; i < 10; i++)
{
cout << endl;
for (int j = 0; j < 12; j++)
{
switch (Field[i][j])
{
case 0: piece = " - "; break;
case space: piece = " "; break;
case peasant: piece = " P "; break;
case -peasant: piece = " EP "; break;
case archer: piece = " A "; break;
case -archer: piece = " EA "; break;
case griffon: piece = " G "; break;
case -griffon: piece = " EG "; break;
case footman: piece = " F "; break;
case -footman: piece = " EF "; break;
case hero: piece = " H "; break;
case -hero: piece = " EH "; break;
}
It gives me back that space, peasant, etc cannot be used in a constant expression
constexpr int peasant = 300;
Should work as written. Show the exact error message and line number that it refers to. And what compiler are you using?
I tried in VS and the error indicates all case lines:
197 IntelliSense: 'this' cannot be used in a constant expression Battlefield.cpp 51 10

Also tried in CodeBlocks where I have the same thing:

|293|error: 'Battlefield::space' cannot appear in a constant-expression|

I tried with constexpr instead of const, but then the errors are:

|242|error: 'constexpr' does not name a type|

|296|error: 'archer' was not declared in this scope|

In VS it even cannot recognize "constexpr"...
Also I have this warning about the same lines:

|257|warning: non-static data member initializers only available with -std=c++11 or -std=gnu++11 [enabled by default]|
"'this' cannot be used in a constant expression" is very true. Is it possible you have member variables or functions that alias your global constants?
It would probably help if you showed the complete class definition and implementation.

Part of your problems seem to be that you are using C++11 constructs but are not using a C++11 compiler.


Oh and never rely on Intellisense errors, always compile your code and refer to the actual compile errors. Intellisense has is known to be wrong in many occasions.

Topic archived. No new replies allowed.