vectorX*::insert() returning X** const

Started by
4 comments, last by nlbs 15 years, 5 months ago

typedef std::map<string, Js::JType*> ElemMap;
ElemMap list;
vector<Js::JType*> clones;
.....
ElemMap::iterator appendClone(const string& key, Js::JType* heapObj){
  return list.insert(make_pair(key, &(*clones.insert(clones.end(), heapObj)) )).first;//Error here in this line
}
The above mentioned line is firing the following error
jobject.h:89: instantiated from here
/usr/include/c++/4.3/bits/stl_pair.h:106: error: cannot convert 'Js::JType** const' to 'Js::JType*' in initialization
I am also including that line of stl_pair.h which means
&(*clones.insert(clones.end(), heapObj))
is returning Js::JType** const Instead of Js::JType*
      template<class _U1, class _U2>
        pair(const pair<_U1, _U2>& __p)
	: first(__p.first),
	  second(__p.second) { }//106
Here is the code for Js::JType::clone()
virtual Js::JType* clone() const {return new Js::JType(*this);}
Advertisement
Blegggh. That is one ugly-ass line of code. Break it into a few lines for readability's sake. FWIW, though, your problem is that you're explicitly finding the address of the just-inserted vector item, despite the fact that what you probably actually want is the item (a pointer) itself.
If I break it into some line it would be:
ElemMap::iterator appendClone(const string& key, Js::JType* heapObj){  Js::JType* __tmp = &(*clones.insert(clones.end(), heapObj));  return list.insert(make_pair(key, __tmp )).first;//Error here in this line}

Now how to solve it.
clones.insert(clones.end(), heapObj)
returns an iterator which is dereferenced by * operator and I am again getting the Js::JType* by referencing it. should I call operator->() Instead of doing it in this way ??
ElemMap::iterator appendClone(const string& key, Js::JType* heapObj){  clones.push_back(heapObj);  return list.insert(make_pair(key, heapObj));}
I think you meant to say return list.insert(make_pair(key, heapObj)).first;
Ya that works . actually I was trying to write terse code in one line. However why my insert() Code is not working ??
according to the manual (If I've not misunderstood something) it should not become const.
It has been fixed as I am storing Object pointers I need to use
return list.insert(make_pair(key, (*clones.insert(clones.end(), heapObj)) )).first;
It was being double referenced.

This topic is closed to new replies.

Advertisement