tags:

views:

64

answers:

3

those are the maps:

multimap<SortKey,T> firstMap;
multimap<SearchKey,pair<SortKey,T>*> secondMap;


template <class T,class SortKey, class SearchKey> bool GarageDataBase<T,SortKey,SearchKey>::Add(T data,SortKey key1, SearchKey key2) 
{
 multimap<SortKey,T>::iterator it;
 it=(firstMap.insert(pair<SortKey,T>(key1,data)));
 pair<SortKey,T> *mizi=&*it;

 secondMap.insert(pair<SearchKey,pair<SortKey,T>*>(key2,mizi));
 return true;
}

I am trying to insert a pair into the firstMap and get a pointer to this pair and insert it into the "second" field in the secondMap

so that i could go into my firstMap from the secondmap.

pair<SortKey,T> *mizi=&*it;

this doesn't compile saying :

error C2440: 'initializing' : cannot convert from 'std::pair<_Ty1,_Ty2> *' to 'std::pair<_Ty1,_Ty2> *'

any idea whats going on or maybe a better way to make it work?

+2  A: 

pair<SortKey,T> is not the same as multimap<SortKey,T>::value_type. The latter is pair<const SortKey,T> since the key is not supposed to change. And since pair<SortKey,T> and pair<const SortKey,T> are not reference-related but two distinct types, the compiler doesn't accept the code. If you account for the const key it should work.

sellibitze
i added the const, still gives the same problem
Nadav
here's the code:
Nadav
ok, this fixed it, thanks
Nadav
A: 
pair<const SortKey,T> *mizi=new pair<const SortKey,T>;
    it=(firstMap.insert(pair<SortKey,T>(key1,data)));
    mizi=&*it;

    secondMap.insert(pair<SearchKey,pair<const SortKey,T>*>(key2,mizi));
    return true;

Ok after changing it to const in insert and in the multimap it seems to work

Nadav
You have a memory leak here. You allocate a pair using new and never delete it.
rlbond
This is just a sample code for testing what i've asked, i'm not gonna use it without deleting all allocations, but thanks
Nadav
A: 

It would be easier to use:

multimap<SortKey,T> firstMap;
multimap<SearchKey,firstMap::iterator> secondMap;


template <class T,class SortKey, class SearchKey> 
bool GarageDataBase<T,SortKey,SearchKey>::Add(T data,SortKey key1, SearchKey key2) 
{
   firstMap::iterator it;
             =firstMap.insert(firstMap::value_type(key1,data));
   secondMap.insert(secondMap::value_type(key2,it));
   return true;
}

This has several advantages: 1. don't have to know internal representation of multimap's types 2. Cannot do something stupid like incrementing the pointers but otherwise syntax is the same SortKey sortkey=secondMap.find(searchkey)->second->first;

Lance Diduck