2017-05-12 58 views
0

2つのスレッド(1つはパーティション用、もう1つはスレッド)を使用してQuicksortを実行するプログラムを作成しようとしています。重要なコードは次のようになります。C++のスレッドが関数呼び出しとstd :: vectorを引数にして

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

である私は、多くの記事やを通じて見えた:

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

私は、私は完全には理解できないコンパイラによってこのエラーが出ますこれを解決するために多くのことを試みましたが、何か間違っているはずです。

+0

なぜ彼らは両方の 'QuickSort'と呼ばれていますか? –

+0

さて、私は関数のオーバーロードに問題はないと考えましたが(最初の定義には異なる引数があります)、別の名前を試しましたが、別のよりわかりにくいエラーを返します... –

+0

それは問題だとは思いますが、それを理解しようとするうちにちょっと混乱させてしまいます。 –

答えて

0

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()); 
+0

または、2つの関数に異なる名前を付けます。 – 1201ProgramAlarm

+0

私はそれを(別のやり方で)解決することができたので、私は質問に答えようとしていました。コンパイルオプションに '-lpthread'を追加し、関数の名前を変更するのは名前の変更だけではないようです。 –

関連する問題