2016-02-25 51 views
0

我想写一个小程序来学习C++ 11多线程程序。所以我决定写一个mergeSort来测试它。C++ 11多线程合并排序错误“没有构造函数的实例std :: thread'匹配参数列表”

这里是我的代码:

class SortMethods 
    { 
    protected: 
     int N; 
     int *aux; 
    public: 
     void mergeSort(int a[]) 
     { 
      aux = new int[N]; 
      mergeSort(a, 0, N - 1); 
     } 
     void merge(int a[], int low, int high) 
     { 
      int mid = (low + high)/2; 

      //optimization 3 for nearly-sorted array 
      //we can add a condition to improve performance when the array has already sorted or nearly-sorted. 
      if (a[mid] <= a[mid + 1]) 
      { 
       return; 
      } 

      int i = low; 
      int j = mid + 1; 

      for (int k = low; k <= high; k++) 
      { 
       aux[k] = a[k]; 
      } 

      for (int k = low; k <= high; k++) 
      { 
       if (i > mid) 
        a[k] = aux[j++]; 
       else if (j > high) 
        a[k] = aux[i++]; 
       else if (lessThan(aux[j], aux[i])) 
        a[k] = aux[j++]; 
       else 
        a[k] = aux[i++]; 
      } 
     } 
     void mergeSort(int a[], int low, int high) 
     { 
      if (high <= low) 
      { 
       return; 
      } 
      int mid = low + (high - low)/2; 

      //single_thread 
      mergeSort(a, low, mid); 
      mergeSort(a, mid + 1, high); 
      /*merge(a, low, high);*/ 

      //multi_thread 
      /*thread left(mergeSort, a, low, mid); 
      thread right(mergeSort, a, mid + 1, high); 
      left.join(); 
      right.join();*/ 

      merge(a, low, high); 

     } 
    } 
int main() 
{ 
    int *a = new int(100); 

    for(int i=0; i<100; i++) 
    { 
     a[i] = rand() % 1000; 
    } 
    SortMethods sort(100); 
    sort.mergeSort(a); 
} 

但是,当我在编译的VS2015的代码,它会抛出一个没有构造“的std ::线程”的实例参数列表相匹配的错误。

你能帮我找到我的代码问题吗?

=============================================

因为你们的帮助,我发现,因为我重载mergeSort方法。我重命名方法mergeSort_multi_thread

thread left(&SortMethods::mergeSort_multi_thread, a, low, mid); 

我得到了错误 没有专门的函数模板“未知类型的std ::调用(_Callable & &,_types & & ......)

1> g:\dataalog\datastructuresandalgo\basic_data_structures\sorting.h(240): note: see reference to function template instantiation 'std::thread::thread<void(__thiscall SortMethods::*)(int [],int,int),int&[],int&,int&,void>(_Fn &&,int &[],int &,int &)' being compiled 
1>   with 
1>   [ 
1>    _Fn=void (__thiscall SortMethods::*)(int [],int,int) 
1>   ] 
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ========== 

[enter image description here][1] 

Thx

+1

您正在使用方法,而不是函数。检查此答案http://stackoverflow.com/questions/10673585/start-thread-with-member-function了解如何传递成员函数及其各自的对象 – Nadir

+2

不要评论导致错误的代码,因为否则人们无法重现错误!发布不起作用的代码!但是,这已经被答复了一百万次。 –

回答

1

你超载mergeSort,所以编译器不知道你的意思。使用static_cast选择正确的一个。

static_cast<void(SortMethods::*)(int[], int, int)>(&SortMethods::mergeSort) 

当您创建线程,你必须通过this太:

std::thread left(static_cast<...>(...), this, a, low, mid); 

它可能给你的错误未能找到匹配的构造函数thread,因为它试图找到过载集的构造函数。如果你看完整的错误信息,它应该说一些关于这个。

+0

而不是'static_cast',你能重命名一个函数吗? (理想情况下,使第一个SortMethods类的构造函数。) –

+0

@MartinBonner是的。超载是最初的问题,因此以任何方式修复都会照顾它。 –

+0

@James Root嗨,我重命名方法,但得到了一个错误,未能专门化功能模板'未知类型std :: invoke(_Callable &&,_类型&& ...)'线程离开(&SortMethods :: mergeSort_multi_thread,a,低,中); –

相关问题