stl::map - can't map int to char * 
Author Message
 stl::map - can't map int to char *

I'm have trouble trying to create a map that maps ints to
char * (actually LPCTSTR)

my class is as follows:
typedef std::map<int, LPCTSTR> MapStatusToLPCTSTR;

class CHttpStatusMap
{
public:
        CHttpStatusMap()
        {
                m_oMap.clear();
                m_oMap.insert(200, "Status OK");
        }

        virtual ~CHttpStatusMap() {}

private:
        MapStatusToLPCTSTR m_oMap;

Quote:
};

and the error is:

httpmsg.h(117) : error C2664: 'class std::_Tree<int,struct
std::pair<int const ,char const *>,struct
std::map<int,char const *,struct std::less<int>,class
std::allocator<char const *> >::_Kfn,struct std::less<
int>,class std::allocator<char const *> >::iterator
__thiscall std::map<int,char const *,struct
std::less<int>,class std::allocator<char const *> >::insert
(class std::_Tree<int,struct std::pair<int const ,char
const *>,struct std::map<int,char const
 *,struct std::less<int>,class std::allocator<char const
*> >::_Kfn,struct std::less<int>,class std::allocator<char
const *> >::iterator,const struct std::pair<int
const ,char const *> &)' : cannot convert parameter 1
from 'const int' to 'class std:
:_Tree<int,struct std::pair<int const ,char const
*>,struct std::map<int,char const *,struct
std::less<int>,class std::allocator<char const *>

Quote:
>::_Kfn,struct std::less<int>,class std::allocator<char

const *> >::iterator'
        No constructor could take the source type, or
constructor overload resolution was ambiguous

The problem is to do with the int but what, I'm not sure.

Any ideas?? Thanks!!



Mon, 19 Jul 2004 20:49:55 GMT  
 stl::map - can't map int to char *
On Thu, 31 Jan 2002 04:49:55 -0800, "Kramer"

Quote:

>I'm have trouble trying to create a map that maps ints to
>char * (actually LPCTSTR)

>my class is as follows:
>typedef std::map<int, LPCTSTR> MapStatusToLPCTSTR;

>class CHttpStatusMap
>{
>public:
>    CHttpStatusMap()
>    {
>            m_oMap.clear();

The clear is unnecessary.

Quote:
>            m_oMap.insert(200, "Status OK");
>    }

I suggest you take a look at the documentation for map::insert. It
takes a map::value_type, which happens to be a pair<const Key,
Value>...

m_oMap.insert(MapStatusToLPCTSTR::value_type(200, "Status OK"));

Tom



Mon, 19 Jul 2004 21:27:58 GMT  
 stl::map - can't map int to char *
It seems like map has no "insert" member with two params where first is key
value and second - element value.

try to use
   m_oMap.insert(MapStatusToLPCTSTR::value_type(200, "Status OK"));
instead of
   m_oMap.insert(200, "Status OK");

it should work.


Quote:
> I'm have trouble trying to create a map that maps ints to
> char * (actually LPCTSTR)

> my class is as follows:
> typedef std::map<int, LPCTSTR> MapStatusToLPCTSTR;

> class CHttpStatusMap
> {
> public:
> CHttpStatusMap()
> {
> m_oMap.clear();
> m_oMap.insert(200, "Status OK");
> }

> virtual ~CHttpStatusMap() {}

> private:
> MapStatusToLPCTSTR m_oMap;
> };

> and the error is:

> httpmsg.h(117) : error C2664: 'class std::_Tree<int,struct
> std::pair<int const ,char const *>,struct
> std::map<int,char const *,struct std::less<int>,class
> std::allocator<char const *> >::_Kfn,struct std::less<
> int>,class std::allocator<char const *> >::iterator
> __thiscall std::map<int,char const *,struct
> std::less<int>,class std::allocator<char const *> >::insert
> (class std::_Tree<int,struct std::pair<int const ,char
> const *>,struct std::map<int,char const
>  *,struct std::less<int>,class std::allocator<char const
> *> >::_Kfn,struct std::less<int>,class std::allocator<char
> const *> >::iterator,const struct std::pair<int
> const ,char const *> &)' : cannot convert parameter 1
> from 'const int' to 'class std:
> :_Tree<int,struct std::pair<int const ,char const
> *>,struct std::map<int,char const *,struct
> std::less<int>,class std::allocator<char const *>
> >::_Kfn,struct std::less<int>,class std::allocator<char
> const *> >::iterator'
>         No constructor could take the source type, or
> constructor overload resolution was ambiguous

> The problem is to do with the int but what, I'm not sure.

> Any ideas?? Thanks!!



Mon, 19 Jul 2004 21:29:15 GMT  
 stl::map - can't map int to char *
D'Oh!!! Thanks.

Quote:
>-----Original Message-----
>On Thu, 31 Jan 2002 04:49:55 -0800, "Kramer"

>>I'm have trouble trying to create a map that maps ints
to
>>char * (actually LPCTSTR)

>>my class is as follows:
>>typedef std::map<int, LPCTSTR> MapStatusToLPCTSTR;

>>class CHttpStatusMap
>>{
>>public:
>>        CHttpStatusMap()
>>        {
>>                m_oMap.clear();

>The clear is unnecessary.

>>                m_oMap.insert(200, "Status OK");
>>        }

>I suggest you take a look at the documentation for
map::insert. It
>takes a map::value_type, which happens to be a pair<const
Key,
>Value>...

>m_oMap.insert(MapStatusToLPCTSTR::value_type(200, "Status
OK"));

>Tom

>.



Mon, 19 Jul 2004 21:59:36 GMT  
 stl::map - can't map int to char *
thanks!!!!

Quote:
>-----Original Message-----
>It seems like map has no "insert" member with two params
where first is key
>value and second - element value.

>try to use
>   m_oMap.insert(MapStatusToLPCTSTR::value_type

(200, "Status OK"));
Quote:
>instead of
>   m_oMap.insert(200, "Status OK");

>it should work.



>> I'm have trouble trying to create a map that maps ints
to
>> char * (actually LPCTSTR)

>> my class is as follows:
>> typedef std::map<int, LPCTSTR> MapStatusToLPCTSTR;

>> class CHttpStatusMap
>> {
>> public:
>> CHttpStatusMap()
>> {
>> m_oMap.clear();
>> m_oMap.insert(200, "Status OK");
>> }

>> virtual ~CHttpStatusMap() {}

>> private:
>> MapStatusToLPCTSTR m_oMap;
>> };

>> and the error is:

>> httpmsg.h(117) : error C2664: 'class

std::_Tree<int,struct

- Show quoted text -

Quote:
>> std::pair<int const ,char const *>,struct
>> std::map<int,char const *,struct std::less<int>,class
>> std::allocator<char const *> >::_Kfn,struct std::less<
>> int>,class std::allocator<char const *> >::iterator
>> __thiscall std::map<int,char const *,struct
>> std::less<int>,class std::allocator<char const *>
>::insert
>> (class std::_Tree<int,struct std::pair<int const ,char
>> const *>,struct std::map<int,char const
>>  *,struct std::less<int>,class std::allocator<char const
>> *> >::_Kfn,struct std::less<int>,class
std::allocator<char
>> const *> >::iterator,const struct std::pair<int
>> const ,char const *> &)' : cannot convert parameter 1
>> from 'const int' to 'class std:
>> :_Tree<int,struct std::pair<int const ,char const
>> *>,struct std::map<int,char const *,struct
>> std::less<int>,class std::allocator<char const *>
>> >::_Kfn,struct std::less<int>,class std::allocator<char
>> const *> >::iterator'
>>         No constructor could take the source type, or
>> constructor overload resolution was ambiguous

>> The problem is to do with the int but what, I'm not
sure.

>> Any ideas?? Thanks!!

>.



Mon, 19 Jul 2004 22:00:32 GMT  
 stl::map - can't map int to char *

Quote:
> I'm have trouble trying to create a map that maps ints to
> char * (actually LPCTSTR)

> my class is as follows:
> typedef std::map<int, LPCTSTR> MapStatusToLPCTSTR;

BTW, In addition to what people have said, be careful mapping to char*'s.
The way you are using it is valid, but you need to be very vigilant that you
don't map to a transient string (one you've built on the stack, for
instance) or any heap based strings (because you won't be able to
differentiate them to free the memory later).  Consider mapping to a string
class instead of just a character pointer.

Ken



Tue, 20 Jul 2004 00:27:42 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. MFC map or stl map

2. map<int,const char*> question

3. map<int,const char*> question

4. Wacky behavior from map<char*, int>

5. Chicago Map's Precision Mapping OCX

6. STL's map<> item reorder

7. OLECHAR's in an STL map container?

8. Strange problem with VC++'s STL map

9. STL's map<> item reorder

10. STL's map

11. Problem with STL Maps in Release Builds exported from DLL's

12. STL Newbie: STL maps

 

 
Powered by phpBB® Forum Software