I''m writing a part of my messaging subsystem. The idea is to take the first 5 bits of the message, which holds the message type identifier and branch based on that. Some code that doesn''t work:
..definition
#define SZ_MESSAGEID 5
struct Header_MessageID
{
unsigned short MessageID : SZ_MESSAGEID;
};
struct Message_Header_Test
//This is used to test the value of the messageID
{
Header_MessageID MessageID;
unsigned short Padding : 3;
};
... and the code
Message_Header_Test CurrentMessage;
memcpy(&CurrentMessage,Data,8);//Copy in the first byte and read out the first X bytes for the messageid
switch (CurrentMessage.MessageID)
{
case MESSAGE_PACKEDCOMPRESSED:
(Oh and structs are 1 bit aligned so thats the reason for the padding)
With this code I get :
C:\Program Files\Microsoft Visual Studio\VC98\myprojects\ListServerClient\Unpacker.cpp(22) : error C2450: switch expression of type ''struct Header_MessageID'' is illegal
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
I would have thought that since I''m using a struct to access the data I could treat it like a normal number.
If this is just wrong could someone suggest a better way of structuring the code?
Many thanks
Chris