Constants definitions
-
Hi all, I was 3 days trying to find out why my vc++ project didn't compile. I always got an error while compiling resources. I also thought it could be a visual studio bug, so I installed the new version, but the problem was still there. When I was about to surrender I had the resources.h file opened, and I watch to the screen a few seconds and saw this: ... #define HWND_MAINWINDOW 15000 #define HWND_NAMEEDIT HWND_MAINWINDOW + 1 #define HOTKEYF8 HWND_MAINWINDOW + 2 #define HOTKEYF9 HWND_MAINWINDOW + 3 ... It deserves a very big :doh: But Visual Studio wasn't too helpful either, the error only appeared when compiling resources. Enjoy.
rotter
-
Hi all, I was 3 days trying to find out why my vc++ project didn't compile. I always got an error while compiling resources. I also thought it could be a visual studio bug, so I installed the new version, but the problem was still there. When I was about to surrender I had the resources.h file opened, and I watch to the screen a few seconds and saw this: ... #define HWND_MAINWINDOW 15000 #define HWND_NAMEEDIT HWND_MAINWINDOW + 1 #define HOTKEYF8 HWND_MAINWINDOW + 2 #define HOTKEYF9 HWND_MAINWINDOW + 3 ... It deserves a very big :doh: But Visual Studio wasn't too helpful either, the error only appeared when compiling resources. Enjoy.
rotter
Sorry, but I don't get it, why wouldn't it compile?
-
Sorry, but I don't get it, why wouldn't it compile?
I didn't know it could generate any problem either but it seems that the compiler gets streaky with this kind of definitions. I made this change and the project compiles perfectly: ... #define HWND_MAINWINDOW 15000 #define HWND_NAMEEDIT 15001 #define HOTKEYF8 15002 #define HOTKEYF9 15003 ... A solution using "static const" definitions instead of "#define" would also worked.
rotter
-
Hi all, I was 3 days trying to find out why my vc++ project didn't compile. I always got an error while compiling resources. I also thought it could be a visual studio bug, so I installed the new version, but the problem was still there. When I was about to surrender I had the resources.h file opened, and I watch to the screen a few seconds and saw this: ... #define HWND_MAINWINDOW 15000 #define HWND_NAMEEDIT HWND_MAINWINDOW + 1 #define HOTKEYF8 HWND_MAINWINDOW + 2 #define HOTKEYF9 HWND_MAINWINDOW + 3 ... It deserves a very big :doh: But Visual Studio wasn't too helpful either, the error only appeared when compiling resources. Enjoy.
rotter
It's important to remember that #define is a text replacement mechanism and that the HWND_MAINWINDOW + 3 get resolved where HOTKEYF9 is used in the code. This can lead to compile or logic errors. Another common mistake is this: x = HOTKEYF9 * 2 will be expanded to x = HWND_MAINWINDOW + 3 * 2. x would be 1500 * 6 and not 1503 * 2 as you would expect. Solution: #define HWND_MAINWINDOW 15000 #define HWND_NAMEEDIT (HWND_MAINWINDOW + 1) #define HOTKEYF8 (HWND_MAINWINDOW + 2) #define HOTKEYF9 (HWND_MAINWINDOW + 3)
-
I didn't know it could generate any problem either but it seems that the compiler gets streaky with this kind of definitions. I made this change and the project compiles perfectly: ... #define HWND_MAINWINDOW 15000 #define HWND_NAMEEDIT 15001 #define HOTKEYF8 15002 #define HOTKEYF9 15003 ... A solution using "static const" definitions instead of "#define" would also worked.
rotter
rotter512 wrote:
A solution using "static const" definitions instead of "#define" would also worked.
Yes, but they waste precious bytes. (Well, I suppose that still matters in a few cases.)
-
I didn't know it could generate any problem either but it seems that the compiler gets streaky with this kind of definitions. I made this change and the project compiles perfectly: ... #define HWND_MAINWINDOW 15000 #define HWND_NAMEEDIT 15001 #define HOTKEYF8 15002 #define HOTKEYF9 15003 ... A solution using "static const" definitions instead of "#define" would also worked.
rotter
Using a #define constant like
#define foo 1234
#define bar foo+1will often work, but may cause problems if foo is used in another expression. For example, "bar*2" will yield 1236 [i.e. 1234+(1*2)] rather than 2470 [i.e. (1234+1)*2]. Rather than using numeric literals for everything, though, it's usually best to write
#define bar (foo+1)
to ensure correct evaluation. I should mention another issue: when coding multi-statement macros that look like void function calls, they should be written as:
#define pseudofunction() do {whatever; you; like;} while(0)
There is no semicolon after the (0). Writing multiple statements in a macro without the enclosing while() will cause it to break if the macro is used as a single statement in an if() statement. The 'while' statement avoids this; the semicolon following the macro invocation will terminate the 'if' clause. Incidentally, another formulation with similar effect is
#define pseudofunction() if (1) {whatever; you; like;} else
I think that will work everywhere the former one does, but if the semicolon is omitted after the macro invocation it will generate bogus code (the 'while(0)' version will generate a compile error in that case).
-
rotter512 wrote:
A solution using "static const" definitions instead of "#define" would also worked.
Yes, but they waste precious bytes. (Well, I suppose that still matters in a few cases.)
-
Hi all, I was 3 days trying to find out why my vc++ project didn't compile. I always got an error while compiling resources. I also thought it could be a visual studio bug, so I installed the new version, but the problem was still there. When I was about to surrender I had the resources.h file opened, and I watch to the screen a few seconds and saw this: ... #define HWND_MAINWINDOW 15000 #define HWND_NAMEEDIT HWND_MAINWINDOW + 1 #define HOTKEYF8 HWND_MAINWINDOW + 2 #define HOTKEYF9 HWND_MAINWINDOW + 3 ... It deserves a very big :doh: But Visual Studio wasn't too helpful either, the error only appeared when compiling resources. Enjoy.
rotter
I had also such problems, but I dont remember clearly under which circumstances. But using a name like HWND_MAINWINDOW is also :doh: :mad:X| The VS has a own character in not accepting some names, which have used keywords as strings in it.:confused:
Greetings from Germany
-
I had also such problems, but I dont remember clearly under which circumstances. But using a name like HWND_MAINWINDOW is also :doh: :mad:X| The VS has a own character in not accepting some names, which have used keywords as strings in it.:confused:
Greetings from Germany