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. make this variable global

make this variable global

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 4 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.
  • S Offline
    S Offline
    sweep123
    wrote on last edited by
    #1

    How can I make this (test_26_tt1) variable global; typedef struct { unsigned short data; unsigned 4 : count unsigned 1 : fault unsigned 2 : err }TEST_26_TT1 // Form a union between the message structure and a buffer union { TEST_26_TT1 message_data; unsigned short buffer[32]; }test_26_tt1; If it was a float then I could do // Globally declare the variable in just before the Main section of the code float flt1; extern float flt1; // This would allow global use in another unit of the program. Or should type type of data be wrapped up in a class; as they are many.

    J H 2 Replies Last reply
    0
    • S sweep123

      How can I make this (test_26_tt1) variable global; typedef struct { unsigned short data; unsigned 4 : count unsigned 1 : fault unsigned 2 : err }TEST_26_TT1 // Form a union between the message structure and a buffer union { TEST_26_TT1 message_data; unsigned short buffer[32]; }test_26_tt1; If it was a float then I could do // Globally declare the variable in just before the Main section of the code float flt1; extern float flt1; // This would allow global use in another unit of the program. Or should type type of data be wrapped up in a class; as they are many.

      J Offline
      J Offline
      Jaime Stuardo
      wrote on last edited by
      #2

      what problem did you have when you used the same way as float? Jaime

      S 1 Reply Last reply
      0
      • J Jaime Stuardo

        what problem did you have when you used the same way as float? Jaime

        S Offline
        S Offline
        sweep123
        wrote on last edited by
        #3

        No problem with the float; Just do extern float flt1; in any units you want to use it. My problem is with the union, how do I reference it in another unit? extern test_26_tt1; I have lots of bit fields which I form into a buffer via the union (same memory, but two names).

        J 1 Reply Last reply
        0
        • S sweep123

          How can I make this (test_26_tt1) variable global; typedef struct { unsigned short data; unsigned 4 : count unsigned 1 : fault unsigned 2 : err }TEST_26_TT1 // Form a union between the message structure and a buffer union { TEST_26_TT1 message_data; unsigned short buffer[32]; }test_26_tt1; If it was a float then I could do // Globally declare the variable in just before the Main section of the code float flt1; extern float flt1; // This would allow global use in another unit of the program. Or should type type of data be wrapped up in a class; as they are many.

          H Offline
          H Offline
          Henry miller
          wrote on last edited by
          #4

          You haven't created a variable yet, just a type extern TEST_26_tt1 my_global_variable; The extern is sometimes optional, but the same rules apply as for float.

          G 1 Reply Last reply
          0
          • S sweep123

            No problem with the float; Just do extern float flt1; in any units you want to use it. My problem is with the union, how do I reference it in another unit? extern test_26_tt1; I have lots of bit fields which I form into a buffer via the union (same memory, but two names).

            J Offline
            J Offline
            Jaime Stuardo
            wrote on last edited by
            #5

            the question I did was not for the float, but for the test_26_tt1. What problem did you have with it? Variable isn'r recognize? did you receive a compilation error? Jaime

            S 1 Reply Last reply
            0
            • J Jaime Stuardo

              the question I did was not for the float, but for the test_26_tt1. What problem did you have with it? Variable isn'r recognize? did you receive a compilation error? Jaime

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

              When you form a union you can now use the data. i.e. two variables have been declared. // Form a union between the message structure and a buffer union { TEST_26_TT1 message_data; unsigned short buffer[32]; }test_26_tt1; in the code you can do the following:- test_26_tt1.buffer[3] = 0xFF; My question was how do you refer to this data in another unit using the extern If I use the statement extern test_26_tt1; and refer to it as unsigned short x = test_26_tt1.buffer[2]; in another unit I get the error:- c:\Example\Database.cpp(101): error C2228: left of '.buffer' must have class/struct/union type. Its a question of type, as I said with float extern float flt1; extern ???? test_26_tt1;

              1 Reply Last reply
              0
              • H Henry miller

                You haven't created a variable yet, just a type extern TEST_26_tt1 my_global_variable; The extern is sometimes optional, but the same rules apply as for float.

                G Offline
                G Offline
                Grahamfff
                wrote on last edited by
                #7

                When you form a union you have declared a variable; i.e. // Form a union between the message structure and a buffer union { TEST_26_TT1 message_data; unsigned short buffer[32]; }test_26_tt1;in the code you can do the following:- test_26_tt1.buffer[3] = 0xFF; But how do you use the variable test_26_tt1 in another unit. With a float you can use: extern float flt; But how do you do the same with test_26_tt1. extern ???? test_26_tt1; What type do you use. grahamfff

                H 1 Reply Last reply
                0
                • G Grahamfff

                  When you form a union you have declared a variable; i.e. // Form a union between the message structure and a buffer union { TEST_26_TT1 message_data; unsigned short buffer[32]; }test_26_tt1;in the code you can do the following:- test_26_tt1.buffer[3] = 0xFF; But how do you use the variable test_26_tt1 in another unit. With a float you can use: extern float flt; But how do you do the same with test_26_tt1. extern ???? test_26_tt1; What type do you use. grahamfff

                  H Offline
                  H Offline
                  Henry miller
                  wrote on last edited by
                  #8

                  I had to look up union again... I think extern union { TEST_26_TT1 message_data; unsigned short buffer[32]; } test_26_tt1; will work. Or make your union a full type like this: union test_26_union { TEST_26_TT1 message_data; unsigned short buffer[32]; } extern test_26_union test_26_tt1; Depending on if you want to use this union elsewhere. Let me know what works, I've never used unions in C++, but I can think of a few times where it might be useful.

                  S 1 Reply Last reply
                  0
                  • H Henry miller

                    I had to look up union again... I think extern union { TEST_26_TT1 message_data; unsigned short buffer[32]; } test_26_tt1; will work. Or make your union a full type like this: union test_26_union { TEST_26_TT1 message_data; unsigned short buffer[32]; } extern test_26_union test_26_tt1; Depending on if you want to use this union elsewhere. Let me know what works, I've never used unions in C++, but I can think of a few times where it might be useful.

                    S Offline
                    S Offline
                    sweep123
                    wrote on last edited by
                    #9

                    Its Monday morning here in the UK, just tried out your suggestion:- extern union { TEST_26_TT1 message_data; unsigned short buffer[32]; } test_26_tt1; It worked OK. I did try the other method you suggested:- union test_26_union { TEST_26_TT1 message_data; unsigned short buffer[32]; }; extern test_26_union test_26_tt1; But I kept getting the following error:- My_Example error LNK2005: "union test_26_union test_26_tt1" (?test_26_tt1@@test_26_union@@A) already defined in Database2.obj But thanks for your post.

                    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