On using uint32_t data type for the key and value in a map. I am getting an error no known conversion for argument.
Here is what I tried
int main() {
uint32_t a = 5;
uint32_t b = 9;
map <uint32_t, uint32_t> Mapper;
Mapper.insert({9,8});
auto imageFoundIt = Mapper.find(make_pair(a, b));
return 0;
}
Here is the error stack trace
g++ /tmp/cpzeeEDm4C.cpp
/tmp/cpzeeEDm4C.cpp: In function 'int main()':
/tmp/cpzeeEDm4C.cpp:10:52: error: no matching function for call to 'std::map<unsigned int, unsigned int>::find(std::pair<unsigned int, unsigned int>)'
10 | auto imageFoundIt = Mapper.find(make_pair(a, b));
| ^
In file included from /usr/include/c++/9/map:61,
from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:81,
from /tmp/cpzeeEDm4C.cpp:3:
/usr/include/c++/9/bits/stl_map.h:1168:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::iterator std::map<_Key, _Tp, _Compare, _Alloc>::find(const key_type&) [with _Key = unsigned int; _Tp = unsigned int; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >; std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const unsigned int, unsigned int> >; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = unsigned int]'
1168 | find(const key_type& __x)
| ^~~~
/usr/include/c++/9/bits/stl_map.h:1168:28: note: no known conversion for argument 1 from 'std::pair<unsigned int, unsigned int>' to 'const key_type&' {aka 'const unsigned int&'}
1168 | find(const key_type& __x)
| ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/9/bits/stl_map.h:1174:2: note: candidate: 'template<class _Kt> decltype (((std::map<_Key, _Tp, _Compare, _Alloc>*)this)->std::map<_Key, _Tp, _Compare, _Alloc>::_M_t._M_find_tr(__x)) std::map<_Key, _Tp, _Compare, _Alloc>::find(const _Kt&) [with _Kt = _Kt; _Key = unsigned int; _Tp = unsigned int; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >]'
1174 | find(const _Kt& __x) -> decltype(_M_t._M_find_tr(__x))
| ^~~~
/usr/include/c++/9/bits/stl_map.h:1174:2: note: template argument deduction/substitution failed:
/usr/include/c++/9/bits/stl_map.h: In substitution of 'template<class _Kt> decltype (((std::map<unsigned int, unsigned int>*)this)->std::map<unsigned int, unsigned int>::_M_t.std::_Rb_tree<unsigned int, std::pair<const unsigned int, unsigned int>, std::_Select1st<std::pair<const unsigned int, unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, unsigned int> > >::_M_find_tr(__x)) std::map<unsigned int, unsigned int>::find<_Kt>(const _Kt&) [with _Kt = std::pair<unsigned int, unsigned int>]':
/tmp/cpzeeEDm4C.cpp:10:52: required from here
/usr/include/c++/9/bits/stl_map.h:1174:2: error: no matching function for call to 'std::_Rb_tree<unsigned int, std::pair<const unsigned int, unsigned int>, std::_Select1st<std::pair<const unsigned int, unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, unsigned int> > >::_M_find_tr(const std::pair<unsigned int, unsigned int>&)'
In file included from /usr/include/c++/9/map:60,
from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:81,
from /tmp/cpzeeEDm4C.cpp:3:
/usr/include/c++/9/bits/stl_tree.h:1306:2: note: candidate: 'template<class _Kt, class _Req> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_find_tr(const _Kt&) [with _Kt = _Kt; _Req = _Req; _Key = unsigned int; _Val = std::pair<const unsigned int, unsigned int>; _KeyOfValue = std::_Select1st<std::pair<const unsigned int, unsigned int> >; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >]'
1306 | _M_find_tr(const _Kt& __k)
| ^~~~~~~~~~
/usr/include/c++/9/bits/stl_tree.h:1306:2: note: template argument deduction/substitution failed:
/usr/include/c++/9/bits/stl_tree.h: In substitution of 'template<class _Cmp, class _SfinaeType> using __has_is_transparent_t = typename std::__has_is_transparent<_Cmp, _SfinaeType>::type [with _Cmp = std::less<unsigned int>; _SfinaeType = std::pair<unsigned int, unsigned int>]':
/usr/include/c++/9/bits/stl_tree.h:1304:9: required by substitution of 'template<class _Kt> decltype (((std::map<unsigned int, unsigned int>*)this)->std::map<unsigned int, unsigned int>::_M_t.std::_Rb_tree<unsigned int, std::pair<const unsigned int, unsigned int>, std::_Select1st<std::pair<const unsigned int, unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, unsigned int> > >::_M_find_tr(__x)) std::map<unsigned int, unsigned int>::find<_Kt>(const _Kt&) [with _Kt = std::pair<unsigned int, unsigned int>]'
/tmp/cpzeeEDm4C.cpp:10:52: required from here
/usr/include/c++/9/bits/stl_tree.h:425:11: error: no type named 'type' in 'struct std::__has_is_transparent<std::less<unsigned int>, std::pair<unsigned int, unsigned int>, void>'
425 | using __has_is_transparent_t
| ^~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/9/bits/stl_map.h: In substitution of 'template<class _Kt> decltype (((std::map<unsigned int, unsigned int>*)this)->std::map<unsigned int, unsigned int>::_M_t.std::_Rb_tree<unsigned int, std::pair<const unsigned int, unsigned int>, std::_Select1st<std::pair<const unsigned int, unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, unsigned int> > >::_M_find_tr(__x)) std::map<unsigned int, unsigned int>::find<_Kt>(const _Kt&) [with _Kt = std::pair<unsigned int, unsigned int>]':
/tmp/cpzeeEDm4C.cpp:10:52: required from here
/usr/include/c++/9/bits/stl_tree.h:1315:2: note: candidate: 'template<class _Kt, class _Req> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_find_tr(const _Kt&) const [with _Kt = _Kt; _Req = _Req; _Key = unsigned int; _Val = std::pair<const unsigned int, unsigned int>; _KeyOfValue = std::_Select1st<std::pair<const unsigned int, unsigned int> >; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >]'
1315 | _M_find_tr(const _Kt& __k) const
| ^~~~~~~~~~
/usr/include/c++/9/bits/stl_tree.h:1315:2: note: template argument deduction/substitution failed:
In file included from /usr/include/c++/9/map:61,
from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:81,
from /tmp/cpzeeEDm4C.cpp:3:
/usr/include/c++/9/bits/stl_map.h:1193:7: note: candidate: 'std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator std::map<_Key, _Tp, _Compare, _Alloc>::find(const key_type&) const [with _Key = unsigned int; _Tp = unsigned int; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >; std::map<_Key, _Tp, _Compare, _Alloc>::const_iterator = std::_Rb_tree_const_iterator<std::pair<const unsigned int, unsigned int> >; std::map<_Key, _Tp, _Compare, _Alloc>::key_type = unsigned int]'
1193 | find(const key_type& __x) const
| ^~~~
/usr/include/c++/9/bits/stl_map.h:1193:28: note: no known conversion for argument 1 from 'std::pair<unsigned int, unsigned int>' to 'const key_type&' {aka 'const unsigned int&'}
1193 | find(const key_type& __x) const
| ~~~~~~~~~~~~~~~~^~~
/usr/include/c++/9/bits/stl_map.h:1199:2: note: candidate: 'template<class _Kt> decltype (((const std::map<_Key, _Tp, _Compare, _Alloc>*)this)->std::map<_Key, _Tp, _Compare, _Alloc>::_M_t._M_find_tr(__x)) std::map<_Key, _Tp, _Compare, _Alloc>::find(const _Kt&) const [with _Kt = _Kt; _Key = unsigned int; _Tp = unsigned int; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >]'
1199 | find(const _Kt& __x) const -> decltype(_M_t._M_find_tr(__x))
| ^~~~
/usr/include/c++/9/bits/stl_map.h:1199:2: note: template argument deduction/substitution failed:
/usr/include/c++/9/bits/stl_map.h: In substitution of 'template<class _Kt> decltype (((const std::map<unsigned int, unsigned int>*)this)->std::map<unsigned int, unsigned int>::_M_t.std::_Rb_tree<unsigned int, std::pair<const unsigned int, unsigned int>, std::_Select1st<std::pair<const unsigned int, unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, unsigned int> > >::_M_find_tr(__x)) std::map<unsigned int, unsigned int>::find<_Kt>(const _Kt&) const [with _Kt = std::pair<unsigned int, unsigned int>]':
/tmp/cpzeeEDm4C.cpp:10:52: required from here
/usr/include/c++/9/bits/stl_map.h:1199:2: error: no matching function for call to 'std::_Rb_tree<unsigned int, std::pair<const unsigned int, unsigned int>, std::_Select1st<std::pair<const unsigned int, unsigned int> >, std::less<unsigned int>, std::allocator<std::pair<const unsigned int, unsigned int> > >::_M_find_tr(const std::pair<unsigned int, unsigned int>&) const'
In file included from /usr/include/c++/9/map:60,
from /usr/include/x86_64-linux-gnu/c++/9/bits/stdc++.h:81,
from /tmp/cpzeeEDm4C.cpp:3:
/usr/include/c++/9/bits/stl_tree.h:1306:2: note: candidate: 'template<class _Kt, class _Req> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_find_tr(const _Kt&) [with _Kt = _Kt; _Req = _Req; _Key = unsigned int; _Val = std::pair<const unsigned int, unsigned int>; _KeyOfValue = std::_Select1st<std::pair<const unsigned int, unsigned int> >; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >]'
1306 | _M_find_tr(const _Kt& __k)
| ^~~~~~~~~~
/usr/include/c++/9/bits/stl_tree.h:1306:2: note: template argument deduction/substitution failed:
/usr/include/c++/9/bits/stl_tree.h:1315:2: note: candidate: 'template<class _Kt, class _Req> std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::const_iterator std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_M_find_tr(const _Kt&) const [with _Kt = _Kt; _Req = _Req; _Key = unsigned int; _Val = std::pair<const unsigned int, unsigned int>; _KeyOfValue = std::_Select1st<std::pair<const unsigned int, unsigned int> >; _Compare = std::less<unsigned int>; _Alloc = std::allocator<std::pair<const unsigned int, unsigned int> >]'
1315 | _M_find_tr(const _Kt& __k) const
| ^~~~~~~~~~
/usr/include/c++/9/bits/stl_tree.h:1315:2: note: template argument deduction/substitution failed:
How can I use uint32_t as key and value here?
The problem is that std::map::find takes the key(which is of type Key) value of the element to search for as argument but you're providing a std::pair and hence the error.
So solve this just replace the call to find with:
//------------------------------v--->pass the key value
auto imageFoundIt = Mapper.find(a)
which will give return an iterator to an element with key equivalent to key. And if no such element is found, past-the-end iterator will be returned.
I was trying to get the element with largest size from a vector of elements, as succinct as possible. The original code looks like,
std::cout << ((*std::max_element(rooms.begin(), rooms.end(),
[](auto a, auto b){a.size() < b.size();})).size()) << std::endl;
which g++ complains,
wall3.cpp: In function ‘int main()’:
wall3.cpp:150:47: error: ‘class __gnu_cxx::__normal_iterator<std::set<std::pair<long unsigned int, long unsigned int> >*, std::vector<std::set<std::pair<long unsigned int, long unsigned int> > > >’ has no member named ‘size’
150 | [](auto a, auto b){a.size() < b.size();}).size() << std::endl;
| ^~~~
In file included from /usr/include/c++/9/bits/stl_algobase.h:71,
from /usr/include/c++/9/bits/char_traits.h:39,
from /usr/include/c++/9/string:40,
from /usr/include/c++/9/bitset:47,
from wall3.cpp:1:
/usr/include/c++/9/bits/predefined_ops.h: In instantiation of ‘constexpr bool __gnu_cxx::__ops::_Iter_comp_iter<_Compare>::operator()(_Iterator1, _Iterator2) [with _Iterator1 = __gnu_cxx::__normal_iterator<std::set<std::pair<long unsigned int, long unsigned int> >*, std::vector<std::set<std::pair<long unsigned int, long unsigned int> > > >; _Iterator2 = __gnu_cxx::__normal_iterator<std::set<std::pair<long unsigned int, long unsigned int> >*, std::vector<std::set<std::pair<long unsigned int, long unsigned int> > > >; _Compare = main()::<lambda(auto:1, auto:2)>]’:
/usr/include/c++/9/bits/stl_algo.h:5692:12: required from ‘constexpr _ForwardIterator std::__max_element(_ForwardIterator, _ForwardIterator, _Compare) [with _ForwardIterator = __gnu_cxx::__normal_iterator<std::set<std::pair<long unsigned int, long unsigned int> >*, std::vector<std::set<std::pair<long unsigned int, long unsigned int> > > >; _Compare = __gnu_cxx::__ops::_Iter_comp_iter<main()::<lambda(auto:1, auto:2)> >]’
/usr/include/c++/9/bits/stl_algo.h:5743:43: required from ‘constexpr _FIter std::max_element(_FIter, _FIter, _Compare) [with _FIter = __gnu_cxx::__normal_iterator<std::set<std::pair<long unsigned int, long unsigned int> >*, std::vector<std::set<std::pair<long unsigned int, long unsigned int> > > >; _Compare = main()::<lambda(auto:1, auto:2)>]’
wall3.cpp:150:45: required from here
/usr/include/c++/9/bits/predefined_ops.h:143:18: error: void value not ignored as it ought to be
143 | { return bool(_M_comp(*__it1, *__it2)); }
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It turns out that I forgot to use return in the lambda expression. Adding return solved the problem.
Lesson: use a different compiler and turn on the strictest warning configurations to reveal the problem faster. The output from clang++ is far more explicit,
wall3.cpp:150:33: warning: relational comparison result unused [-Wunused-comparison]
[](auto a, auto b){a.size() < b.size();})).size()) << std::endl;
~~~~~~~~~^~~~~~~~~~
I've looked at similar problems but I simply can not see what is going wrong.
I've tried ::const_iterator as well. But gcc will not compile either way.
it's about : i = allFunctions.erase(i);
void removeEventListener(const std::string &type, function listener){
if(!hasEventListener(type))
return;
std::map<int, std::list<function> > &allFunctions = eventHandlerList[type];
std::map<int, std::list<function > >::iterator i;
for(i=allFunctions.begin(); i!=allFunctions.end(); ++i)
{
i->second.remove(listener);
if(i->second.empty())
{
i = allFunctions.erase(i);
}
}
if(allFunctions.empty())
eventHandlerList.erase(type);
}
error with const_iterator:
Error: passing const std::list<void (*)(const st::event::Event&)> as
this argument of void std::list<_Tp, _Alloc>::remove(const
value_type&) [with _Tp = void (*)(const st::event::Event&), _Alloc =
std::allocator<void (*)(const st::event::Event&)>, std::list<_Tp,
_Alloc>::value_type = void (*)(const st::event::Event&)] discards qualifiers [-fpermissive]
Error: no matching function for call to std::map<int, std::list<void
(*)(const st::event::Event&)> >::erase(std::map<int, std::list<void
(*)(const st::event::Event&)> >::const_iterator&)
error with iterator:
no match for operator= in i = (& allFunctions)->std::map<_Key, _Tp, _Compare, _Alloc>::erase [with _Key = int, _Tp = std::list<void (*)(const st::event::Event&)>, _Compare = std::less<int>, _Alloc = std::allocator<std::pair<const int, std::list<void (*)(const st::event::Event&)> > >, std::map<_Key, _Tp, _Compare, _Alloc>::iterator = std::_Rb_tree_iterator<std::pair<const int, std::list<void (*)(const st::event::Event&)> > >](i)
Any suggestions?
Try this general associative-container erasure loop:
for (std::map<int, std::list<function> >::iterator it = allFunctions.begin();
it != allFunctions.end(); /* no increment! */ )
{
it->second.remove(listener);
if (it->second.empty()) { allFunctions.erase(it++); }
else { ++it; }
}
C++11 changes the signature and return type of member-erase functions, and then you can have a single line it = allFunctions.erase(it);.
I have a template function (as follows) in a namespace called myNamespace:
template <typename setX>
void getRandomItems(NaturalNumber size, setX &random, setX &items)
{
assert(size <= items.size());
//set of randomly selected indices for items
set<NaturalNumber> index;
NaturalNumber r, i;
while(index.size() < size)
{
r = unifRand(0,items.size()-1);
index.insert(r);
}
typename setX::iterator it, sit = items.begin();
for(i = 0, it = index.begin(); it != index.end(); it ++)
{
//find the r-th elt in index
r = *it;
for(; i < r; i ++)
sit++;
random.insert(*sit);
}
}
However whenever I call this function I get these errors:
generic.h: In function ‘void myNamespace::getRandomItems(NaturalNumber, setX&, setX&) [with setX = std::set<std::basic_string<char> >, NaturalNumber = long unsigned int]’:
synthetic-graph.C:87:55: instantiated from here
generic.h:74:32: error: no match for ‘operator=’ in ‘it = index.std::set::begin [with _Key = long unsigned int, _Compare = std::less<long unsigned int>, _Alloc = std::allocator<long unsigned int>, std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<long unsigned int>]()’
/usr/include/c++/4.5/bits/stl_tree.h:224:5: note: candidate is: std::_Rb_tree_const_iterator<std::basic_string<char> >& std::_Rb_tree_const_iterator<std::basic_string<char> >::operator=(const std::_Rb_tree_const_iterator<std::basic_string<char> >&)
synthetic-graph.C:87:55: instantiated from here
generic.h:74:32: error: no match for ‘operator!=’ in ‘it != index.std::set<_Key, _Compare, _Alloc>::end [with _Key = long unsigned int, _Compare = std::less<long unsigned int>, _Alloc = std::allocator<long unsigned int>, std::set<_Key, _Compare, _Alloc>::iterator = std::_Rb_tree_const_iterator<long unsigned int>]()’
/usr/include/c++/4.5/bits/stl_tree.h:291:7: note: candidate is: bool std::_Rb_tree_const_iterator<_Tp>::operator!=(const std::_Rb_tree_const_iterator<_Tp>::_Self&) const [with _Tp = std::basic_string<char>, std::_Rb_tree_const_iterator<_Tp>::_Self = std::_Rb_tree_const_iterator<std::basic_string<char> >]
generic.h:77:4: error: cannot convert ‘const std::basic_string<char>’ to ‘NaturalNumber’ in assignment
I have tried all combinations but no luck, please help me!!!
setX is not a set of NaturalNumbers so the iterators aren't compatible when you say it = index.begin(). You could possibly make it an iterator of set<NaturalNumber> instead, I can't quite make out what you really want to do here.
Also I noticed that in your inner loop you don't do any checks to make sure sit doesn't run off the end of its set.
You're trying to assign incompatible iterators.
Perhaps you meant
set<NaturalNumber>::iterator it;
typename setX::iterator sit = items.begin();
instead of
typename setX::iterator it, sit = items.begin();
I'm writing a parser using Bison, and am using a map for semantic evaluation.
In one of the functions I have the following code:
map<int, int>* result = new map<int, int>();
map<int, int>::iterator liter;
map<int, int>::iterator riter;
liter = lval.polyMap.begin();
riter = rval.polyMap.begin();
l = liter->first;
r = riter->first;
(*result).insert(l, (booleanCondition()) ? liter->second : -liter->second);
(polyMap is of type map<int, int>).
And this gives me the following errors:
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_tree.h: In member functio
n `void std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::insert_unique(
_II, _II) [with _InputIterator = int, _Key = int, _Val = std::pair<const int, in
t>, _KeyOfValue = std::_Select1st<std::pair<const int, int> >, _Compare = std::l
ess<int>, _Alloc = std::allocator<std::pair<const int, int> >]':
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_map.h:397: instantiated
from `void std::map<_Key, _Tp, _Compare, _Alloc>::insert(_InputIterator, _Input
Iterator) [with _InputIterator = int, _Key = int, _Tp = int, _Compare = std::les
s<int>, _Alloc = std::allocator<std::pair<const int, int> >]'
polynom.ypp:147: instantiated from here
/usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/stl_tree.h:996: error: invali
d type argument of `unary *'
Where polynom.ypp line 147 contains (*result).insert(l, (booleanCondition()) ? liter->second : -liter->second);
What's the problem?
You might want to call (*result).insert(make_pair(l, (booleanCondition()) ? liter->second : -liter->second));