make this variable global
-
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. -
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.what problem did you have when you used the same way as float? Jaime
-
what problem did you have when you used the same way as float? Jaime
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).
-
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.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.
-
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).
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
-
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
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 theextern
If I use the statementextern test_26_tt1;
and refer to it asunsigned 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 floatextern float flt1;
extern ???? test_26_tt1;
-
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.
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 -
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. grahamfffI 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.
-
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.
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.