[RESOLVED]using enum in #if preprocessor directive [modified]
-
Hi, Based on the value 'SELECTED' I am trying to do some statements. The following code doesnt work for the two values of 'SELECTED'.
typedef enum{
APPLE =0,
ORANGE =1
}FRUITS;#define SELECTED APPLE
#if SELECTED == 1
AfxMessageBox("orange");
#else
AfxMessageBox("apple");
#endifKindly help.
Priya Sundar
modified on Tuesday, May 19, 2009 6:04 AM
-
Hi, Based on the value 'SELECTED' I am trying to do some statements. The following code doesnt work for the two values of 'SELECTED'.
typedef enum{
APPLE =0,
ORANGE =1
}FRUITS;#define SELECTED APPLE
#if SELECTED == 1
AfxMessageBox("orange");
#else
AfxMessageBox("apple");
#endifKindly help.
Priya Sundar
modified on Tuesday, May 19, 2009 6:04 AM
#if
is a pre-processor directive processed by the preprocessor andenum
andAfxMessageBox
are handled by the compiler. You cannot mix them this way. What is it that you're trying to do here?«_Superman_» I love work. It gives me something to do between weekends.
-
Hi, Based on the value 'SELECTED' I am trying to do some statements. The following code doesnt work for the two values of 'SELECTED'.
typedef enum{
APPLE =0,
ORANGE =1
}FRUITS;#define SELECTED APPLE
#if SELECTED == 1
AfxMessageBox("orange");
#else
AfxMessageBox("apple");
#endifKindly help.
Priya Sundar
modified on Tuesday, May 19, 2009 6:04 AM
Priya_Sundar wrote:
#define SELECTED APPLE
As you've heard you can't do that. In the above line APPLE is not defined so SELECTED remains undefined so you always get
Priya_Sundar wrote:
AfxMessageBox("apple");
You can only use
#define APPLE 0
#define ORANGE 1 -
Priya_Sundar wrote:
#define SELECTED APPLE
As you've heard you can't do that. In the above line APPLE is not defined so SELECTED remains undefined so you always get
Priya_Sundar wrote:
AfxMessageBox("apple");
You can only use
#define APPLE 0
#define ORANGE 1Thanks All... by defining it as 0 and 1 its resolved.
Priya Sundar
-
#if
is a pre-processor directive processed by the preprocessor andenum
andAfxMessageBox
are handled by the compiler. You cannot mix them this way. What is it that you're trying to do here?«_Superman_» I love work. It gives me something to do between weekends.
«_Superman_» wrote:
You cannot mix them this way.
Tell me one thing if I replace enum with #define and instead of AfxMessageBox, I define varA, then it compiles.
#define APPLE 0
#define ORANGE 1
#define SELECTED APPLE#if SELECTED == 1
int varA = 100;
#else
int varA = 101;
#endifReplacing enum with #define is fine but isn't the definition of varA also handled by compiler only? I mean why
int varA = 101;
is ok andAfxMessageBox
not? -
«_Superman_» wrote:
You cannot mix them this way.
Tell me one thing if I replace enum with #define and instead of AfxMessageBox, I define varA, then it compiles.
#define APPLE 0
#define ORANGE 1
#define SELECTED APPLE#if SELECTED == 1
int varA = 100;
#else
int varA = 101;
#endifReplacing enum with #define is fine but isn't the definition of varA also handled by compiler only? I mean why
int varA = 101;
is ok andAfxMessageBox
not?I bet you have put that globally. I mean outside of all functions. You cannot call a function globally unless you are assigning to a variable. Try this.
#if SELECTED == 1
int varA = AfxMessageBox(L"Selected");
#else
int varA = AfxMessageBox(L"Not Selected");
#endif«_Superman_» I love work. It gives me something to do between weekends.
-
«_Superman_» wrote:
You cannot mix them this way.
Tell me one thing if I replace enum with #define and instead of AfxMessageBox, I define varA, then it compiles.
#define APPLE 0
#define ORANGE 1
#define SELECTED APPLE#if SELECTED == 1
int varA = 100;
#else
int varA = 101;
#endifReplacing enum with #define is fine but isn't the definition of varA also handled by compiler only? I mean why
int varA = 101;
is ok andAfxMessageBox
not?#define APPLE 0
#define ORANGE 1int varA = 0;
#define APPLE_DEFINED
#ifdef APPLE_DEFINED
varA = 100;
#else if ORANGE_DEFINED
varA = 101;
#else if PAPAYA_DEFINED
varA = 102;
#endif //APPLE_DEFINEDIt is a crappy thing, but it's life -^ Carlo Pallini
-
I bet you have put that globally. I mean outside of all functions. You cannot call a function globally unless you are assigning to a variable. Try this.
#if SELECTED == 1
int varA = AfxMessageBox(L"Selected");
#else
int varA = AfxMessageBox(L"Not Selected");
#endif«_Superman_» I love work. It gives me something to do between weekends.
-
Yeah! u r correct! But why so? I mean why is it ok if I assign it to an int and not otherwise?
It isn't about a function call actually. It is about initializing a global variable even if it means calling a function to do it. So we can use that trick to execute a function even before the entry point of a program.
«_Superman_» I love work. It gives me something to do between weekends.
-
It isn't about a function call actually. It is about initializing a global variable even if it means calling a function to do it. So we can use that trick to execute a function even before the entry point of a program.
«_Superman_» I love work. It gives me something to do between weekends.