• ### Announcements

#### Archived

This topic is now archived and is closed to further replies.

# bitfield members in a switch

## Recommended Posts

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

#### Share this post

##### Share on other sites
errhh, what are you doing ??

structs definily not bit aligned !!

and you switch on a struct, it does not now how to compare with the case's...i dont now if overloading the operator == helps...or if it is posibile with structs...

or do you want to switch on the short in Header_MessageID ??
then change it to
switch (CurrentMessage.MessageID.MessageID)

Edited by - claus hansen ries on June 15, 2000 7:54:00 AM

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627676
• Total Posts
2978582

• 11
• 12
• 10
• 12
• 22