Extended Bitvectors

Posted by Greven on Thu 15 Jan 2004 06:19 PM — 14 posts, 31,206 views.

Canada #0
I'm trying to install extended bitvectors into my SWR, and I'd like to be able to print out all the available flags currently in a flag array, so that you don't have to update the send_to_char list in build.c every time, cut down a little bit of work. This method worked fine with flag_string, I simple put
flag_string(-1, rflags)
for example, and it would print out the whole array. Buts since ext_flag_string is slightly different, this method doesn't work. So I though, ok, lets just set the for loop to loop the amount of elements are in the flag sting times(if its got 10 elements, loop 10 times). My question is this: Does anyone know how to determine the amount of elements in, for example,
char *	const	r_flags	[]
? Or is there a better way to do this?
Amended on Thu 15 Jan 2004 06:20 PM by Greven
USA #1
You're working in C++, aren't you?

If that's the case, you can create an STL vector instead, which has almost the exact same properties as an array, with the added bonus of being able to use the .size() method.

Peronsally I know of no way to get the size of an array declared like that. You could always just have an int r_flags_size that needs to be incremented when you add a flag to the array... shouldn't be hard to remember if you put it right next to it. That way you'd have the size, at least.

Still, I would say C++ is the way to go. :)
Australia Forum Administrator #2
To find the number of elements in an array like that I usually use this define:


#define NUMITEMS(arg) (sizeof (arg) / sizeof (arg [0]))


eg.


const char * test [] = { "nick", "gammon" };

int i = NUMITEMS (test); // i will be 2
Canada #3
Nick, your a genius, thanks!
USA #4
Now I feel stupid for forgetting that... doubly stupid because it's just what I had to teach after posting! Shame on me... *bonk self*!! I had that nagging feeling that I was forgetting something, but couldn't put my finger on it. *grin*

I still do think that C++ is the way to go, however. :P
Canada #5
I was using (sizeof(flagarray)/sizeof(flagarray[0])), but in the function, since it a pointer to the array, it wasn't working right, and I couldn't think through how to do it. Thank, works wonderfully.
Canada #6
Ok, appears that I have come across another problem with the extended bitvectors. I am getting this error on compilation:
olc.h:86: field `body_parts' has incomplete type
I've never seen this error before, but everything seems to be of with the line
	int			death_age;
	EXT_BV		body_parts;
	/* No more hard coding */
I'm also getting errors about it not body_parts not being in the structure. The only thing I can think that it might possibly be is that this is in a different header file, not mud.h, and that the extended bitvectors were not being seen by the olc.h file. I really have no idea, I might have done something wrong when bringing it into my code. Any ideas?

PS - does anyone happen to know of how to lookup error messages? A website perhaps? Thanks

PPS - I have the include for olc.h after the declaration of the EXT_BV in mud.h
Amended on Fri 16 Jan 2004 06:19 AM by Greven
USA #7
You could have header files including each other, or header files included from program source files.

Try doing a make clean, then make again, just to be safe.

You could also include mud.h at the top of olc.h, as long as both files have include guards!
Canada #8
I checked, and the only place olc.h is included in in mud.h. I did a make clean, and just got the same error for every file that included mud.h. I also added the guards and included mud.h in olc.h, but to no avail, and since I don't really understand the error, I dunno what to do.
Amended on Fri 16 Jan 2004 06:56 AM by Greven
Australia Forum Administrator #9
Check that the file that defines EXT_BV is included before olc.h.
USA #10
It means that when the compiler encountered olc.h, it hadn't yet encountered the definition for EXT_BV in the previous parts of mud.h.

It needs to encounter 2 things before olc.h is included:


typedef struct	extended_bitvector	EXT_BV;


and


struct extended_bitvector
{
   unsigned int bits[XBI];
};
Canada #11
EXT_BV is defined in mud.h, and the "typedef struct extended_bitvector EXT_BV;" is defined before the include for olc.h. Does it matter if the actual structure is below the include?

[EDIT] I just moved the include below the structure, and it worked fine. Thanks alot, muchly appreciated, to everyone:)[/EDIT]
Amended on Fri 16 Jan 2004 07:04 AM by Greven
Australia Forum Administrator #12
Quote:

olc.h:86: field `body_parts' has incomplete type


Glad that worked. There is a difference between a declaration and a definition. As Sampson said, the following lines *declare* EXT_BV...


typedef struct extended_bitvector EXT_BV;


Thus, the compiler knows that EXT_BV is *something*.

However until it sees the definition:



struct extended_bitvector
{
   unsigned int bits[XBI];
};


It doesn't know what EXT_BV actually is, and thus doesn't know how much memory to allocate for it. Thus the error message 'incomplete type'.
Canada #13
Ah, thanks, I'm glad you could clear that up, certainly helps me in the future :)