[英]C++ Error: no matching function for call to 'std::vector<int>::swap(std::vector<int>)'
[英]C++ threads not matching function call with std::vector as argument
我正在尝试编写一个使用两个线程(一个用于分区,另一个用于另一个线程)执行Quicksort的程序。 重要的代码如下所示:
// Function that makes the partitions
size_t Divide(std::vector<int> &v, size_t ini, size_t fin){
// Not so important code...
}
// Function called by threads
void QuickSort(std::vector<int> &v, size_t ini, size_t fin){
// Not so important code...
}
// Initial partition
void QuickSort(std::vector<int> &v){
size_t division = Divide(v, 0, v.size());
std::thread p1(QuickSort, std::ref(v), 0, division);
std::thread p2(QuickSort, std::ref(v), division+1, v.size());
p1.join();
p2.join();
}
我由编译器收到此错误,我无法完全理解:
thrtest.cpp: In function ‘void QuickSort(std::vector<int>&)’:
thrtest.cpp:57:54: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, std::reference_wrapper<std::vector<int> >, int, size_t&)’
std::thread p1(QuickSort, std::ref(v), 0, division);
^
In file included from thrtest.cpp:7:0:
/usr/include/c++/5/thread:133:7: note: candidate: template<class _Callable, class ... _Args> std::thread::thread(_Callable&&, _Args&& ...)
thread(_Callable&& __f, _Args&&... __args)
^
/usr/include/c++/5/thread:133:7: note: template argument deduction/substitution failed:
thrtest.cpp:57:54: note: couldn't deduce template parameter ‘_Callable’
std::thread p1(QuickSort, std::ref(v), 0, division);
^
In file included from thrtest.cpp:7:0:
/usr/include/c++/5/thread:128:5: note: candidate: std::thread::thread(std::thread&&)
thread(thread&& __t) noexcept
^
/usr/include/c++/5/thread:128:5: note: candidate expects 1 argument, 4 provided
/usr/include/c++/5/thread:122:5: note: candidate: std::thread::thread()
thread() noexcept = default;
^
/usr/include/c++/5/thread:122:5: note: candidate expects 0 arguments, 4 provided
thrtest.cpp:58:63: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, std::reference_wrapper<std::vector<int> >, size_t, std::vector<int>::size_type)’
std::thread p2(QuickSort, std::ref(v), division+1, v.size());
^
In file included from thrtest.cpp:7:0:
/usr/include/c++/5/thread:133:7: note: candidate: template<class _Callable, class ... _Args> std::thread::thread(_Callable&&, _Args&& ...)
thread(_Callable&& __f, _Args&&... __args)
^
/usr/include/c++/5/thread:133:7: note: template argument deduction/substitution failed:
thrtest.cpp:58:63: note: couldn't deduce template parameter ‘_Callable’
std::thread p2(QuickSort, std::ref(v), division+1, v.size());
^
In file included from thrtest.cpp:7:0:
/usr/include/c++/5/thread:128:5: note: candidate: std::thread::thread(std::thread&&)
thread(thread&& __t) noexcept
^
/usr/include/c++/5/thread:128:5: note: candidate expects 1 argument, 4 provided
/usr/include/c++/5/thread:122:5: note: candidate: std::thread::thread()
thread() noexcept = default;
^
/usr/include/c++/5/thread:122:5: note: candidate expects 0 arguments, 4
provided
编译为g++ -g -O2 -std=c++14 thrtest.cpp -o thrtest
我浏览了许多帖子,并尝试了很多方法来解决此问题,但是我一定弄错了。
QuickSort
是一个重载函数,您必须QuickSort
转换它:
auto quick_sort = static_cast<void (*)(std::vector<int> &, size_t, size_t)>(QuickSort);
std::thread p1(quick_sort, std::ref(v), 0, division);
std::thread p2(quick_sort, std::ref(v), division+1, v.size());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.