www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Associative Arrays with classes and structs?

reply Daniel Horn <hellcatv hotmail.com> writes:
so I want to make an associative array with a struct (a class would 
substitute, but a struct would be better):
the following code does not build with a linker error

assoc.obj(assoc)
  Error 42: Symbol Undefined __init_26TypeInfo_S5assoc9strstruct
--- errorlevel 1

and for the class, I was not sure how to define a proper hashing 
operator.  if I use the following snippit, the hashtable will print out 
0 because the hash of bc is not the same as the hash of ac (I assume 
it's using pointer hashing)

I looked through the documentation to find help about this but the 
associative arrays section was more or less empty aside from the built 
in arrays and types.
I guess I could write a function that hashes and do
c[ac.myhash()]  but that's kludgy and non obvious for people coming from C++

struct strstruct {
	char []b;
}
class strclass {
	char []b;
}

int main (char[][] argv) {
	int [strclass] c;	
	int [strstruct] b;
	strstruct as,bs;
	strclass ac;
	strclass bc;
	ac= new strclass();
	bc= new strclass();
	ac.b="hello";
	bc.b="hello";
	as.b="hello";
	bs.b="hello";	
	b[bs]=3;  //comment to allow link
	printf ("%d\n",b[as]); //comment to allow link
	c[bc]=3;
	printf ("%d\n",c[ac]);	
	return 0;
}
May 20 2004
parent Stewart Gordon <smjg_1998 yahoo.com> writes:
Daniel Horn wrote:
 so I want to make an associative array with a struct (a class would 
 substitute, but a struct would be better):
 the following code does not build with a linker error
 
 assoc.obj(assoc)
  Error 42: Symbol Undefined __init_26TypeInfo_S5assoc9strstruct
 --- errorlevel 1

It's been a known bug for ages. D/23332
 and for the class, I was not sure how to define a proper hashing 
 operator. 

uint toHash() Stewart. -- My e-mail is valid but not my primary mailbox, aside from its being the unfortunate victim of intensive mail-bombing at the moment. Please keep replies on the 'group where everyone may benefit.
May 21 2004