case expression not constant
Nov 10, 2008 at 10:20am UTC
Hello,
My program structure is:
Constants.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#ifndef CONSTANTS_H
#define CONSTANTS_H
class Constants
{
public :
Constants();
~Constants();
static const int8 MAIN_REQ_CLASS_ID;
static const int8 BYTE;
static const int8 SHORT;
};
Constants.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#include "Constants.h"
const int8 Constants::MAIN_REQ_CLASS_ID = 127;
const int8 Constants::BYTE = 1;
const int8 Constants::SHORT = 2;
Constants::Constants()
{
}
Constants::~Constants()
{
}
Main.cpp
1 2 3 4 5 6 7 8 9 10 11 12
int main()
{
int temp=0;
switch (temp)
{
case Constants::BYTE:
break ;
case Constants::SHORT:
break ;
}
return 0;
}
I keep getting compile time error message as "case expression not constant".
Though i have defined the values BYTE,SHORT as constants Constants.cpp
Is there something that i am missing.
help appreciated
amal
Nov 10, 2008 at 1:57pm UTC
I don't understand the situation competely, but if you initialize your constant members in Main.cpp, it will work. I think, this is a restriction of programming language.
If you are not bound to do as you've done, you also may use enumeration inside your class declaration:
1 2 3 4 5 6 7 8
class Constants
{
public :
Constants();
~Constants();
enum MY_CONSTANTS{MAIN_REQ_CLASS_ID=127, BYTE=1, SHORT=2};
};
Topic archived. No new replies allowed.