Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. The Weird and The Wonderful
  4. Constants definitions

Constants definitions

Scheduled Pinned Locked Moved The Weird and The Wonderful
helpcsharpc++visual-studioannouncement
9 Posts 7 Posters 28 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dr Emmett Brown
    wrote on last edited by
    #1

    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

    S F K 3 Replies Last reply
    0
    • D Dr Emmett Brown

      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

      S Offline
      S Offline
      Steve Hansen
      wrote on last edited by
      #2

      Sorry, but I don't get it, why wouldn't it compile?

      D 1 Reply Last reply
      0
      • S Steve Hansen

        Sorry, but I don't get it, why wouldn't it compile?

        D Offline
        D Offline
        Dr Emmett Brown
        wrote on last edited by
        #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

        P S 2 Replies Last reply
        0
        • D Dr Emmett Brown

          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

          F Offline
          F Offline
          Frank Dawson
          wrote on last edited by
          #4

          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)

          1 Reply Last reply
          0
          • D Dr Emmett Brown

            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

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            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.)

            K 1 Reply Last reply
            0
            • D Dr Emmett Brown

              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

              S Offline
              S Offline
              supercat9
              wrote on last edited by
              #6

              Using a #define constant like

              #define foo 1234
              #define bar foo+1

              will 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).

              1 Reply Last reply
              0
              • P PIEBALDconsult

                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.)

                K Offline
                K Offline
                KarstenK
                wrote on last edited by
                #7

                That isnt true. AFAIK clever compiler are knowing this case.

                Greetings from Germany

                1 Reply Last reply
                0
                • D Dr Emmett Brown

                  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

                  K Offline
                  K Offline
                  KarstenK
                  wrote on last edited by
                  #8

                  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

                  C 1 Reply Last reply
                  0
                  • K KarstenK

                    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

                    C Offline
                    C Offline
                    CurtD
                    wrote on last edited by
                    #9

                    KarstenK wrote:

                    But using a name like HWND_MAINWINDOW is also

                    Yeah, it looks like someone is defining window's handles. Which I don't get.

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups