Jump to content
  • Advertisement


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


bitfield members in a switch

This topic is 6670 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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)
(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

Link to 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

Share this post

Link to post
Share on other sites

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!