CString inside CMap
-
When I wrote:
typedef CMap MyType; MyType myType;
I got the following error: error C2440: 'type cast' : cannot convert from 'CString' to 'DWORD_PTR' do you know why ? Thanks in advance Udi Raz -- modified at 11:03 Sunday 11th December, 2005 -
Your declaration is invalid, probably because you have tried to use the <> signs without ticking the 'Ignore HTML tags in this message'.
I Dream of Absolute Zero
-
This is just a guess, but I think the message means it cannot convert your CString class to a unique hash key. If you try:
CMap<CString*, CString*&, int, int&>
Then that compiles. You just have to remember to delete your allocated references. It probably uses the object's address as the hash key. Personally, would use the stl's map class.
I Dream of Absolute Zero
-
When I wrote:
typedef CMap MyType; MyType myType;
I got the following error: error C2440: 'type cast' : cannot convert from 'CString' to 'DWORD_PTR' do you know why ? Thanks in advance Udi Raz -- modified at 11:03 Sunday 11th December, 2005 -
When I wrote:
typedef CMap MyType; MyType myType;
I got the following error: error C2440: 'type cast' : cannot convert from 'CString' to 'DWORD_PTR' do you know why ? Thanks in advance Udi Raz -- modified at 11:03 Sunday 11th December, 2005I think the second parameter needs to be a CString*. However, you'd do better to use std::map, CMap is hideous. Christian Graus - Microsoft MVP - C++
-
When I wrote:
typedef CMap MyType; MyType myType;
I got the following error: error C2440: 'type cast' : cannot convert from 'CString' to 'DWORD_PTR' do you know why ? Thanks in advance Udi Raz -- modified at 11:03 Sunday 11th December, 2005If you look at the definition of CMap, you'll see the following:
CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>
The KEY and VALUE are the data types you're mapping. In your case, KEY = CString and VALUE = int. The ARG_ types are the types used in argument passing within CMap. The code that calculates the hash used for lookups is found in the GetAssocAt function of the CMap template:CMap<KEY, ARG_KEY, VALUE, ARG_VALUE>::GetAssocAt(ARG_KEY key, UINT& nHashBucket, UINT& nHashValue) const
The exact line of code that calcuates the hash is here:nHashValue = HashKey<ARG_KEY>(key);
With your map declaration, the line translates to:nHashValue = HashKey<CString>(key);
If you search for HashKey in the Visual Studio folder, you'll see that Microsoft has defined a handful of HashKey functions for various data types:UINT HashKey(WORD key) const; UINT HashKey(void* key) const; UINT HashKey(void* key) const; UINT HashKey(WORD key) const; UINT HashKey(LPCTSTR key) const; UINT HashKey(LPCTSTR key) const; UINT HashKey(LPCTSTR key) const; etc...
You'll also notice that there isn't a HashKey defined for CString. This is why you're getting the 'cannot convert' error. The compiler does not know how to convert from a CString to a DWORD_PTR, which is the default implementation of HashKey. In the case of CString, there are two ways to solve the problem: 1) Define your own custom HashKey routine for CString, as described in the article http://support.microsoft.com/default.aspx?scid=kb;en-us;158541 2) The simple solution - use LPCTSTR (which has a HashKey provided by Microsoft, and has a casting operator defined in the CString class) as your argument key in your CMap declaration:CMap<CString, LPCTSTR, int, int> myMap;
Bernie (Boom Boom) Geoffrion worked Atlanta Flames games in the 1970s with the splendid Jiggs McDonald. One night, Geoffrion said, "Jiggs, there are only three things to hockey: shooting and skating." McDonald said, "Right, Boomer. And what's the third?" The exasperated Geoffrion replied," Jiggs, that's the three. Shooting. And. Skating." -
When I wrote:
typedef CMap MyType; MyType myType;
I got the following error: error C2440: 'type cast' : cannot convert from 'CString' to 'DWORD_PTR' do you know why ? Thanks in advance Udi Raz -- modified at 11:03 Sunday 11th December, 2005udiraz wrote:
typedef CMap MyType; MyType myType;
thats why i prefer STL:: MAP
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV