samedi 4 avril 2015

c++ error on inserting into unorderd map _ template

I am tring to insert a value into an unordered map and getting the following error:


removeduplicate.cpp:106:69: error: no matching function for call to ‘make_pair(int&, int)’


/usr/include/c++/4.8/bits/stl_pair.h:276:5: note: template constexpr std::pair::__type, typename std::__decay_and_strip<_T2>::__type> std::make_pair(_T1&&, _T2&&) make_pair(_T1&& __x, _T2&& __y)


/usr/include/c++/4.8/bits/stl_pair.h:276:5: note: template argument deduction/substitution failed:


removeduplicate.cpp:106:69: note: cannot convert ‘temp->List::node::data’ (type ‘int’) to type ‘int&&’ if ( !Storage.insert( std::make_pair< T , int >(temp->data , 1 ) .second )){


following is the code:



template < class T >class List{
private:
struct node{
T data;
node *next;
};


node* root;
std::unordered_map< T , int>Storage;
public:
List():root( NULL){}
~List(){}
node *getNode( T);
bool insertNode();
bool traverseList();
int removeDuplicate();
};


the following is the method---



template< class T > int List<T>::removeDuplicate(){
node * temp;
node* prev;
prev = NULL;
temp = root;
typename std::unordered_map< T , int>::iterator it;
int returnVal;
returnVal = 0;


while ( temp ){
it =Storage.find( temp->data );
if ( it != Storage.end() ){
prev->next = temp->next;
returnVal ++;
}
else{


the following line throwing error.



if ( !Storage.insert( std::make_pair< T , int >(temp->data , 1 ) .second )){
std::cerr<<"Could'nt able to inser in std::unordered_map< T , int>]n"<<std::endl;
returnVal = -9999;
}
}
prev = temp;
temp= temp->next;
}

return returnVal;
}

Aucun commentaire:

Enregistrer un commentaire