3

In my header file, I'm declaring a map like so:

std::map<LPD3DXSPRITE, LPDIRECT3DTEXTURE9> sprites; 

In my C++ file, I am trying to insert like so:

sprites.insert(sprite, texture); 

The types I am passing to sprites.insert are correct. Why can I not insert this way? What is the proper way to insert? When I do this, the error I get is like this (snipped):

error C2664: 'std::_Tree<_Traits>::iterator std::_Tree<_Traits>::insert(std::_Tree<_Traits>::iterator,const std::pair<_Ty1,_Ty2> &)' : cannot convert parameter 1 from 'LPD3DXSPRITE' to 'std::_Tree<_Traits>::iterator'

3
  • What are the declared types of sprite and texture ? Commented Nov 24, 2009 at 0:13
  • "The types I am passing to sprites.insert are correct" ORLY? ;-) Commented Nov 24, 2009 at 0:53
  • @Steve the types matched the types I'd assigned in my map :) Commented Nov 24, 2009 at 3:35

2 Answers 2

10

You need to wrap your key and value in an std::pair object:

sprites.insert(std::make_pair(sprite, texture)); 

This is because std::map is a Pair Associative Container. The value_type of std::map<K,V> is std::pair<const K,V>.

Sign up to request clarification or add additional context in comments.

Comments

3

Alternatively, you could use operator[]. For std::map, this will autovivify for you. Depending on what you are doing, it may make calling std::map::insert unnecessary.

1 Comment

insert will check if the value already exists in map, operator[] sets the value for key without checking so it depends on what functionality you need

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.