简体   繁体   English

C ++线程与以std :: vector为参数的函数调用不匹配

[英]C++ threads not matching function call with std::vector as argument

I'm trying to write a program that executes Quicksort using two threads (one for a partition and one for another). 我正在尝试编写一个使用两个线程(一个用于分区,另一个用于另一个线程)执行Quicksort的程序。 The important code looks like this: 重要的代码如下所示:

// 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();
}

I get this error by the compiler, which I can't fully understand: 我由编译器收到此错误,我无法完全理解:

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

Compilation is g++ -g -O2 -std=c++14 thrtest.cpp -o thrtest 编译为g++ -g -O2 -std=c++14 thrtest.cpp -o thrtest

I looked through many posts and tried many things to solve this, but I must be getting something wrong. 我浏览了许多帖子,并尝试了很多方法来解决此问题,但是我一定弄错了。

QuickSort is an overloaded function, you have to cast it: 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());

没有匹配的 function 调用 'std::vector <std.. '< div><div id="text_translate"><p> 我正在解决的问题是:我必须采用<strong>T</strong>测试用例。 For each test case I have to take string as an input, then I need to arrange the input string as: <em>string at even position {double space} string at odd position</em> (example: <em>input</em> - StackOverflow, <em>output</em> - <strong>Sakvrlw</strong> <strong>tcOefo</strong> ). 我编写了以下代码,其中我为所有测试用例获取输入并将其存储在向量中。 然后我将 vector 的元素分配给另一个声明的 string s。</p><pre> #include &lt;cmath&gt; #include &lt;cstdio&gt; #include &lt;vector&gt; #include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;string&gt; using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int T,i; cout &lt;&lt; "Enter no. of test cases: "; cin &gt;&gt; T; vector&lt;string&gt; v; vector&lt;string&gt; odd; vector&lt;string&gt; even; string str; for(int i=0; i&lt;T; i++){ cin &gt;&gt; str; v.push_back(str); } string s; for(i=0; i&lt;v.size(); i++){ s = v.at(i); for(i=0; i&lt;s.size(); i++){ if(i==0){ even.push_back(s[i]); //*This is where I am getting error*. }else if(i==1){ odd.push_back(s[i]); }else{ if(i%2==0){ even.push_back(s[i]); }else{ odd.push_back(s[i]); } } } for(i=0; i&lt;even.size(); i++){ cout &lt;&lt; even.at(i); } cout &lt;&lt; " "; for(i=0; i&lt;odd.size(); i++){ cout &lt;&lt; odd.at(i); } cout &lt;&lt; endl; even.clear(); odd.clear(); s.clear(); } return 0; }</pre><p> 在编译上面的代码时,我得到"no matching error for call std::vector..." 。 <strong><em>我到底做错了什么?</em></strong></p></div></std..> - no matching function for call to 'std::vector<std.. '

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++错误:没有匹配的函数可以调用&#39;std :: vector <int> ::交换(标准::矢量 <int> )” - C++ Error: no matching function for call to 'std::vector<int>::swap(std::vector<int>)' C ++函数的可选std :: vector参数 - C++ Optional std::vector argument for function C ++模板错误:调用std :: vector没有匹配功能 <int, std::allocator<int> &gt; - C++ Templates Error: no matching function for call std::vector<int, std::allocator<int> > C++ - 在 foreach 中复制向量给出“没有匹配的函数来调用 std::vector<int> ::push_back(std::vector)<int> &amp;)” - C++ - copying vector in foreach gives "No matching function to call for std::vector<int>::push_back(std::vector<int>&)" C ++没有匹配函数来调用向量中的擦除 - C++ no matching function for call to erase in vector 错误:没有匹配的函数来调用“std::vector”<x> ::push_back(y&amp;) 在 C++ - error: no matching function for call to ‘std::vector<x>::push_back(y&) in C++ 独立 std::threads 的 C++ std::vector - C++ std::vector of independent std::threads 没有匹配的 function 调用 'std::vector <std.. '< div><div id="text_translate"><p> 我正在解决的问题是:我必须采用<strong>T</strong>测试用例。 For each test case I have to take string as an input, then I need to arrange the input string as: <em>string at even position {double space} string at odd position</em> (example: <em>input</em> - StackOverflow, <em>output</em> - <strong>Sakvrlw</strong> <strong>tcOefo</strong> ). 我编写了以下代码,其中我为所有测试用例获取输入并将其存储在向量中。 然后我将 vector 的元素分配给另一个声明的 string s。</p><pre> #include &lt;cmath&gt; #include &lt;cstdio&gt; #include &lt;vector&gt; #include &lt;iostream&gt; #include &lt;algorithm&gt; #include &lt;string&gt; using namespace std; int main() { /* Enter your code here. Read input from STDIN. Print output to STDOUT */ int T,i; cout &lt;&lt; "Enter no. of test cases: "; cin &gt;&gt; T; vector&lt;string&gt; v; vector&lt;string&gt; odd; vector&lt;string&gt; even; string str; for(int i=0; i&lt;T; i++){ cin &gt;&gt; str; v.push_back(str); } string s; for(i=0; i&lt;v.size(); i++){ s = v.at(i); for(i=0; i&lt;s.size(); i++){ if(i==0){ even.push_back(s[i]); //*This is where I am getting error*. }else if(i==1){ odd.push_back(s[i]); }else{ if(i%2==0){ even.push_back(s[i]); }else{ odd.push_back(s[i]); } } } for(i=0; i&lt;even.size(); i++){ cout &lt;&lt; even.at(i); } cout &lt;&lt; " "; for(i=0; i&lt;odd.size(); i++){ cout &lt;&lt; odd.at(i); } cout &lt;&lt; endl; even.clear(); odd.clear(); s.clear(); } return 0; }</pre><p> 在编译上面的代码时,我得到"no matching error for call std::vector..." 。 <strong><em>我到底做错了什么?</em></strong></p></div></std..> - no matching function for call to 'std::vector<std.. ' C ++ 11没有匹配的函数来调用&#39;std :: vector - C++11 no matching function for call to ‘std::vector 接收std :: vector作为参数的C ++模板函数 - C++ template function that receives std::vector as argument
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM