1

I'm trying to implement a Dijkstra algorithm and I want to override compare function in my set called queue. Can't figure out why it doesn't compile.
Code:

std::vector<int> d(size(), INT32_MAX);
d[vStart] = 0;

std::set<int, decltype([&d](int a, int b) {
    return d[a] < d[b];
})> queue;

Compilator output:

[1/2] Building CXX object CMakeFiles/LW2.dir/Graph.cpp.o
FAILED: CMakeFiles/LW2.dir/Graph.cpp.o 
/usr/bin/c++   -g -std=gnu++20 -MD -MT CMakeFiles/LW2.dir/Graph.cpp.o -MF CMakeFiles/LW2.dir/Graph.cpp.o.d -o CMakeFiles/LW2.dir/Graph.cpp.o -c /home/rostislove/Programming/LW2/Graph.cpp
In file included from /usr/include/c++/11/set:60,
                 from /home/rostislove/Programming/LW2/Graph.cpp:9:
/usr/include/c++/11/bits/stl_tree.h: In instantiation of ‘std::_Rb_tree_key_compare<_Key_compare>::_Rb_tree_key_compare() [with _Key_compare = Graph::getMaxDistance(int)::<lambda(int, int)>]’:
/usr/include/c++/11/bits/stl_tree.h:671:22:   required from ‘std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>::_Rb_tree_impl<_Key_compare, <anonymous> >::_Rb_tree_impl() [with _Key_compare = Graph::getMaxDistance(int)::<lambda(int, int)>; bool <anonymous> = true; _Key = int; _Val = int; _KeyOfValue = std::_Identity<int>; _Compare = Graph::getMaxDistance(int)::<lambda(int, int)>; _Alloc = std::allocator<int>]’
/usr/include/c++/11/bits/stl_tree.h:928:7:   required from here
/usr/include/c++/11/bits/stl_tree.h:149:9: error: use of deleted function ‘Graph::getMaxDistance(int)::<lambda(int, int)>::<lambda>()’
  149 |       : _M_key_compare()
      |         ^~~~~~~~~~~~~~~~
/home/rostislove/Programming/LW2/Graph.cpp:129:32: note: a lambda closure type has a deleted default constructor
  129 |     std::set<int, decltype([dff](int a, int b) {
      |                                ^
ninja: build stopped: subcommand failed.```

0 Answers0