代码之家  ›  专栏  ›  技术社区  ›  minecraftplayer1234

C++线程没有重载函数取x参数

  •  0
  • minecraftplayer1234  · 技术社区  · 6 年前

    我在程序中启动线程时遇到问题。我有一节课,看起来像这样:

    class quicksort {
    private:
        // Array parameters
        int length;
        // Actual sorting functions
        template <typename T>
        void _sort(T* data, int, int);
        template <typename T>
        int _partition(T* data, int, int);
        template <typename T>
        void _swap(T* data, int, int);
        void test_partition(int* data, int length);
    
    public:
        // Constructors
        quicksort() {}
    
        // Sorting functions
        template <typename T>
        void sort(T* data, int len);
    
        void test();
    };
    

    这个 _sort() 方法如下所示:

    template <typename T>
    void quicksort::_sort(T* data, int p, int r) {
        if (p < r) {
            auto q = _partition(data, p, r);
            std::thread lower(&quicksort::_sort, this, data, p, q - 1);
            std::thread upper(&quicksort::_sort, this, data, q + 1, r);
            lower.join();
            upper.join();
        }
    }
    

    当我编译这个时,我得到一个错误:

    C:\Users\Frynio\Dropbox\Studia\ZSSK\Projekt\quicksort\include/quicksort.hpp(55): error C2661: 'std::thread::thread': no overloaded function takes 5 arguments
    C:\Users\Frynio\Dropbox\Studia\ZSSK\Projekt\quicksort\include/quicksort.hpp(41): note: see reference to function template instantiation 'void quicksort::_sort<T>(T *,int,int)' being compiled
            with
            [
                T=int
            ]
    ../src/main.cpp(8): note: see reference to function template instantiation 'void quicksort::sort<int>(T *,int)' being compiled
            with
            [
                T=int
            ]
    C:\Users\Frynio\Dropbox\Studia\ZSSK\Projekt\quicksort\include/quicksort.hpp(56): error C2661: 'std::thread::thread': no overloaded function takes 5 arguments
    

    data 属于类型 T

    1 回复  |  直到 6 年前
        1
  •  1
  •   Alex Reinking    6 年前

    我想你是想(和我一起)写作 <T>

    std::thread lower(&quicksort::_sort<T>, this, data, p, q - 1);
    std::thread upper(&quicksort::_sort<T>, this, data, q + 1, r);
    

    否则编译器将如何推断 _sort 你想传给 std::thread 建造师?事实上,当我用clang 7.0编译您的代码时,我会得到以下额外的错误:

    thread:118:7: note: candidate template ignored: couldn't infer template argument '_Callable'
          thread(_Callable&& __f, _Args&&... __args)
          ^
    

    所以这表明它不能确定 &quicksort::sort .