首页 > 解决方案 > 编译器错误出现在一台特定的机器上

问题描述

我有一个基于 C++ 线程的小型应用程序,它可以在我的带有 G++ 7.3.0 的 Windows 机器上编译和正常工作。但是,相同的 .cpp 文件无法在另一台 Linux 机器上编译,同样使用 G++ 7.3.0。

我相信原因一定是我不知道的编译器选项。特别是,我已经尝试指定 -std 版本,但它并没有改变任何东西。

因为我相信这不依赖于代码,所以我没有发布代码,但如果它对某人有帮助,我会发布

这是我在第二台机器上收到的错误消息:

In file included from ./parallel_prefix_sum.cpp:4:0:
./parallel_executor.cpp: In instantiation of ‘void parallel_executor<T>::test_overhead() [with T = int]’:
./parallel_prefix_sum.cpp:24:19:   required from here
./parallel_executor.cpp:152:29: error: invalid use of non-static member function ‘void parallel_executor<T>::upsweep(std::vector<T>&, int, int, int) [with T = int]’
      threads.push_back(std::thread(upsweep, this, std::ref(input_vector), thread_partition_up[i], thread_partition_up[i+1], i%4));
                             ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./parallel_executor.cpp:79:8: note: declared here
   void upsweep(std::vector<T>& v, int begin, int stop, int d) {
        ^~~~~~~

Edit2:这是一个 MRE

parallel_prefix_sum.cpp

#include <iostream>
#include <chrono>
#include <vector>
#include "parallel_executor.cpp"
using namespace std;

int sum (int a, int b){
    return a+b;
}

int main(){
    int size = 100;
    vector<int> my_input;
    for (int i = 0; i < size; i++) {
        my_input.push_back(i);
    }
    parallel_executor<int>* s = new parallel_executor<int>(my_input, sum, 1);
    s->test_overhead();
    return 0;
}

并行执行器.cpp

#include <functional>
#include <vector>
#include <math.h>
#include <thread>
#include <chrono>

using namespace std;

template<class T> class parallel_executor{
    //public:
        vector<T> input_vector;
        function<T(T,T)> input_function;
        int workers;
        int size;

    public:
        parallel_executor(vector<T> v, function<T(T,T)> f, int w){
            for (int i = 0; i < v.size(); ++i){
                input_vector.push_back(v[i]);
            }
            input_function = f;
            workers = w;
            size = input_vector.size();
        }

        void upsweep(std::vector<T>& v, int begin, int stop, int d) {
            for (int i = begin + pow(2, d) - 1; i < stop && i < size; i += pow(2,d+1)) {
                int j = i + pow(2, d);
                v[j] = input_function(v[i], v[j]);
            }
        }

        void test_overhead(){
            std::vector<std::thread> threads;
            std::vector<int> thread_partition_up{0,size};
                for(int i = 0; i < thread_partition_up.size(); ++i){
                    threads.push_back(std::thread(upsweep, this, std::ref(input_vector), thread_partition_up[i], thread_partition_up[i+1], 1));
                }
            for(int i = 0; i < threads.size(); ++i){
                threads[i].join();
            }
        }
};

标签: c++g++

解决方案


您需要一个 & 和一个范围名称

threads.push_back(std::thread(&parallel_executor<T>::upsweep, this, std::ref(input_vector), thread_partition_up[i], thread_partition_up[i+1], 1));
//                            ^^^^^^^^^^^^^^^^^^^^^^^

如果您的代码被 MinGW 接受,那就是实现错误


推荐阅读