Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CString inside CMap

CString inside CMap

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    udiraz
    wrote on last edited by
    #1

    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

    R J C J T 5 Replies Last reply
    0
    • U udiraz

      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

      R Offline
      R Offline
      RChin
      wrote on last edited by
      #2

      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

      U 1 Reply Last reply
      0
      • R RChin

        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

        U Offline
        U Offline
        udiraz
        wrote on last edited by
        #3

        OK, Thanks, fixed it.. Udi Raz

        R 1 Reply Last reply
        0
        • U udiraz

          OK, Thanks, fixed it.. Udi Raz

          R Offline
          R Offline
          RChin
          wrote on last edited by
          #4

          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

          1 Reply Last reply
          0
          • U udiraz

            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

            J Offline
            J Offline
            JonEngle
            wrote on last edited by
            #5

            you need to use CMap MyType;

            1 Reply Last reply
            0
            • U udiraz

              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

              C Offline
              C Offline
              Christian Graus
              wrote on last edited by
              #6

              I 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++

              1 Reply Last reply
              0
              • U udiraz

                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

                J Offline
                J Offline
                Jack Puppy
                wrote on last edited by
                #7

                If 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."

                1 Reply Last reply
                0
                • U udiraz

                  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

                  T Offline
                  T Offline
                  ThatsAlok
                  wrote on last edited by
                  #8

                  udiraz 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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups