stl map error error C2440

S

swtsvn

hey all,
I am attempting to use map with VC 2005. where i need to map one
integer value to another integer
for e.g 2:5
4:5
the actual use is to map a common int value with direct input values.

here is the code

SN_Input.cpp


//initializes the map
void SN_Input::init_keymap(){
//vector
vector<int>* vec=new vector<int>;
vec->push_back(KEYBOARD_PARAM::LEFT_ARROW);
vec->push_back(DIK_LEFT);

keyMap=new map<int,int>();
keyMap->insert(vec->begin(),vec->end());
}

// function to see if a key is pressed or not, called from a different
file game.cpp
bool SN_Input::isKeyPressed(int key){
int value=keyMap[KEYBOARD_PARAM::LEFT_ARROW];
if(fDIKeyboardState[value] & 0x80){
return TRUE;
}
return FALSE;
}


it gives me the following error:
1>c:\users\suja\documents\visual studio 2005\projects\sujatha-ase-
project\sujatha-ase-project\sn_input.cpp(197) : error C2440:
'initializing' : cannot convert from 'std::map<_Kty,_Ty>' to 'int *'
1> with
1> [
1> _Kty=int,
1> _Ty=int
1> ]
1> No user-defined-conversion operator available that can
perform this conversion, or the operator cannot be called

can anyone please tell me how to correct this error?
i googled but not much info avail.
 
A

Alf P. Steinbach

* swtsvn:
hey all,
I am attempting to use map with VC 2005. where i need to map one
integer value to another integer
for e.g 2:5
4:5
the actual use is to map a common int value with direct input values.

here is the code

SN_Input.cpp


//initializes the map
void SN_Input::init_keymap(){

It's generally ungood to have separate initialization routines. But how to avoid
that depends on your concrete program.

//vector
vector<int>* vec=new vector<int>;
vec->push_back(KEYBOARD_PARAM::LEFT_ARROW);
vec->push_back(DIK_LEFT);

You don't have to dynamically allocate a vector.

And in fact you have a memory leak here because you decided to allocate the
vector dynamically.

Anyway, the vector is not needed.

keyMap=new map<int,int>();
keyMap->insert(vec->begin(),vec->end());

Most probably keyMap doesn't need to be dynamically allocated any more than the
vector above.

When keyMap directly is a 'map<int, int>' all of the above code, since the start
of the routine, reduces to

keyMap[KEYBOARD_PARAM::LEFT_ARROW] = DIK_LEFT;

}

// function to see if a key is pressed or not, called from a different
file game.cpp
bool SN_Input::isKeyPressed(int key){
int value=keyMap[KEYBOARD_PARAM::LEFT_ARROW];
if(fDIKeyboardState[value] & 0x80){
return TRUE;
}
return FALSE;
}

Don't use 'TRUE' and 'FALSE' when C++ has perfectly good 'true' and 'false'. But
don't use 'true' and 'false' either. Just use a boolean expression.

When keyMap directly is a 'map<int, int>' you can write the above as

bool SN_Input::isKeyPressed( int )
{
return !!(fDIKeyboardState[keyMap[KEYBOARD_PARAM::LEFT_ARROW] & 0x80);
}

But it seems to me that the function argument should be used for something, or
there should be no argument?

it gives me the following error:
1>c:\users\suja\documents\visual studio 2005\projects\sujatha-ase-
project\sujatha-ase-project\sn_input.cpp(197) : error C2440:
'initializing' : cannot convert from 'std::map<_Kty,_Ty>' to 'int *'
1> with
1> [
1> _Kty=int,
1> _Ty=int
1> ]
1> No user-defined-conversion operator available that can
perform this conversion, or the operator cannot be called

Please see the FAQ item about how to post a question about Code That Does Not Work.

Those who read your article do not know what line 197 of your code is.


Cheers & hth.,

- Alf
 
L

LR

swtsvn said:
void SN_Input::init_keymap(){
//vector
vector<int>* vec=new vector<int>;
vec->push_back(KEYBOARD_PARAM::LEFT_ARROW);
vec->push_back(DIK_LEFT);

keyMap=new map<int,int>();
keyMap->insert(vec->begin(),vec->end());

Along with the other good advice you've gotten else thread, I just
wanted to add that if you intended to use the second element of the
std::vector pointed to by vec, *vec->end() is not what you want to get
the value of that element.

You may want to consider using std::vector::back for this purpose.
keyMap->insert( std::make_pair(*vec->begin(),vec->back()) );

This might make your intent clearer:
keyMap->insert( std::make_pair(vec->front(), vec->back()) );

Alternatively, you may want to use std::vector::eek:perator[]
keyMap->insert( std::make_pair(*vec->begin(),vec->operator[](1)) );

All of this assumes that you did in fact want DIK_LEFT to have the key
KEYBOARD_PARAM::LEFT_ARROW.


I agree that it's probably not the best idea to use pointers and new
here, but perhaps there is some good reason for it? However, if you
must use pointers it might be better to look into some sort of smart
pointer that will delete whatever was newed when its dtor gets called.

Also, if you're initializing the std::map to the same values each time,
there may be easier ways to accomplish this. I expect this kind of thing
to be even easier with the next standard, but in the mean time, please
consider if one of these examples would make your intent clearer,

(I just picked some constants to show how these can be initialized,
beware magic numbers.)

static const std::pair<int,int> x[] = {
std::make_pair(100,200),
std::make_pair(0,2),
};
static const std::map<int,int> m(x, x+sizeof(x)/sizeof(x[0]) );

Or,

typedef std::map<int,int> MyMap;
static const MyMap::value_type x[] = {
std::make_pair(100,200),
std::make_pair(0,2),
};
static const MyMap m(x, x+sizeof(x)/sizeof(x[0]) );



LR
 
B

Bo Persson

swtsvn said:
hey all,
I am attempting to use map with VC 2005. where i need to map one
integer value to another integer
for e.g 2:5
4:5
the actual use is to map a common int value with direct input
values.

here is the code

SN_Input.cpp


//initializes the map
void SN_Input::init_keymap(){
//vector
vector<int>* vec=new vector<int>;
vec->push_back(KEYBOARD_PARAM::LEFT_ARROW);
vec->push_back(DIK_LEFT);

keyMap=new map<int,int>();
keyMap->insert(vec->begin(),vec->end());

Here it seems like keyMap is a pointer.
}

// function to see if a key is pressed or not, called from a
different file game.cpp
bool SN_Input::isKeyPressed(int key){
int value=keyMap[KEYBOARD_PARAM::LEFT_ARROW];

Here it does not. Is this line 197?
if(fDIKeyboardState[value] & 0x80){
return TRUE;
}
return FALSE;
}


it gives me the following error:
1>c:\users\suja\documents\visual studio 2005\projects\sujatha-ase-
project\sujatha-ase-project\sn_input.cpp(197) : error C2440:
'initializing' : cannot convert from 'std::map<_Kty,_Ty>' to 'int *'
1> with
1> [
1> _Kty=int,
1> _Ty=int
1> ]
1> No user-defined-conversion operator available that can
perform this conversion, or the operator cannot be called


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,062
Latest member
OrderKetozenseACV

Latest Threads

Top