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. General Programming
  3. C / C++ / MFC
  4. macro function compiles error,can anybody explain why?3x!

macro function compiles error,can anybody explain why?3x!

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
6 Posts 5 Posters 0 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.
  • J Offline
    J Offline
    joypain
    wrote on last edited by
    #1

    Hi,guys,I did a test on a macro function code, and compiled it in VC6.0, but the compiler reported an error: "D:\vc project\testmacro.c(7) : error C2105: '--' needs l-value" Would someone explain why line 7 reports an error ,but line 6 doesnt't? 3x! :) Source code is as follows:

    #include "stdio.h"

    #define ABSOLUTE(a) (((a) > 0) ? (a) : (-a)) /* Get absolute value */

    void main()
    {
    int a = -1;
    int c = ABSOLUTE(1); //line 6
    int b = ABSOLUTE(-1); //line 7
    }

    H K C 3 Replies Last reply
    0
    • J joypain

      Hi,guys,I did a test on a macro function code, and compiled it in VC6.0, but the compiler reported an error: "D:\vc project\testmacro.c(7) : error C2105: '--' needs l-value" Would someone explain why line 7 reports an error ,but line 6 doesnt't? 3x! :) Source code is as follows:

      #include "stdio.h"

      #define ABSOLUTE(a) (((a) > 0) ? (a) : (-a)) /* Get absolute value */

      void main()
      {
      int a = -1;
      int c = ABSOLUTE(1); //line 6
      int b = ABSOLUTE(-1); //line 7
      }

      H Offline
      H Offline
      Humayun Kabir Hemoo
      wrote on last edited by
      #2

      Make it #define ABSOLUTE(a) (a > 0 ? a : -1*a) by the way there is already a macro defined as "ABSOLUTE" in wingdi.h ;)

      Md. Humayuon Kabir Hemoo

      S 1 Reply Last reply
      0
      • J joypain

        Hi,guys,I did a test on a macro function code, and compiled it in VC6.0, but the compiler reported an error: "D:\vc project\testmacro.c(7) : error C2105: '--' needs l-value" Would someone explain why line 7 reports an error ,but line 6 doesnt't? 3x! :) Source code is as follows:

        #include "stdio.h"

        #define ABSOLUTE(a) (((a) > 0) ? (a) : (-a)) /* Get absolute value */

        void main()
        {
        int a = -1;
        int c = ABSOLUTE(1); //line 6
        int b = ABSOLUTE(-1); //line 7
        }

        K Offline
        K Offline
        Karthik Kalyanasundaram
        wrote on last edited by
        #3

        Try using this

        #define ABSOLUTE(a) (((a) > 0) ?(a) : -(a))
        

        Do more work Make more mistakes Learn more things

        1 Reply Last reply
        0
        • J joypain

          Hi,guys,I did a test on a macro function code, and compiled it in VC6.0, but the compiler reported an error: "D:\vc project\testmacro.c(7) : error C2105: '--' needs l-value" Would someone explain why line 7 reports an error ,but line 6 doesnt't? 3x! :) Source code is as follows:

          #include "stdio.h"

          #define ABSOLUTE(a) (((a) > 0) ? (a) : (-a)) /* Get absolute value */

          void main()
          {
          int a = -1;
          int c = ABSOLUTE(1); //line 6
          int b = ABSOLUTE(-1); //line 7
          }

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          What you have to understand about macro is that they are simply a 'text replacement': the precompiler will replace occurances of ABSOLUTE in your code by the macro definition, before the compilation. So, it means that ABSOLUTE(-1) will be replaced by:

          (((-1) < 0) ? (-1) : (--1))

          As you can see, the --1 is not valid. This doesn't happen for line 6 because it will be replaced by:

          (((1) < 0) ? (1) : (-1))

          which is valid.

          Cédric Moonen Software developer
          Charting control [v1.4] OpenGL game tutorial in C++ [Part 2 online]

          J 1 Reply Last reply
          0
          • H Humayun Kabir Hemoo

            Make it #define ABSOLUTE(a) (a > 0 ? a : -1*a) by the way there is already a macro defined as "ABSOLUTE" in wingdi.h ;)

            Md. Humayuon Kabir Hemoo

            S Offline
            S Offline
            super_ttd
            wrote on last edited by
            #5

            quite. never forget to use parenthesis everywhere within the macro definition...


            1 Reply Last reply
            0
            • C Cedric Moonen

              What you have to understand about macro is that they are simply a 'text replacement': the precompiler will replace occurances of ABSOLUTE in your code by the macro definition, before the compilation. So, it means that ABSOLUTE(-1) will be replaced by:

              (((-1) < 0) ? (-1) : (--1))

              As you can see, the --1 is not valid. This doesn't happen for line 6 because it will be replaced by:

              (((1) < 0) ? (1) : (-1))

              which is valid.

              Cédric Moonen Software developer
              Charting control [v1.4] OpenGL game tutorial in C++ [Part 2 online]

              J Offline
              J Offline
              joypain
              wrote on last edited by
              #6

              Cool, that's absolutely right! Thank you for your explanations, and other guys also.

              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