在一台特定的计算机上显示编译器错误

时间:2019-06-10 08:14:21

标签: c++ g++

我有一个基于C ++线程的小型应用程序,可以在装有G ++ 7.3.0的Windows计算机上编译并正常工作。但是,完全相同的.cpp文件无法在另一台使用G ++ 7.3.0的Linux机器上进行编译。

我相信原因一定是我不知道的编译器选项。特别是,我已经尝试指定-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;
}

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

1 个答案:

答案 0 :(得分:2)

You need an ampersand and a scope name

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接受,请that's an implementation bug