簡體   English   中英

C ++線程與以std :: vector為參數的函數調用不匹配

[英]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());

沒有匹配的 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 ++函數的可選std :: vector參數 C ++模板錯誤:調用std :: vector沒有匹配功能 <int, std::allocator<int> &gt; C++ - 在 foreach 中復制向量給出“沒有匹配的函數來調用 std::vector<int> ::push_back(std::vector)<int> &amp;)” C ++沒有匹配函數來調用向量中的擦除 錯誤:沒有匹配的函數來調用“std::vector”<x> ::push_back(y&amp;) 在 C++ 獨立 std::threads 的 C++ std::vector 沒有匹配的 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..> C ++ 11沒有匹配的函數來調用&#39;std :: vector 接收std :: vector作為參數的C ++模板函數
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM