gpt4 book ai didi

c++ - 编译器错误出现在一台特定机器上

转载 作者:行者123 更新时间:2023-11-28 01:20:28 24 4
gpt4 key购买 nike

我有一个基于 C++ 线程的小型应用程序,它可以在我的 Windows 机器上使用 G++ 7.3.0 编译并正常运行。但是,同一个 .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();
}
}
};

最佳答案

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 .

关于c++ - 编译器错误出现在一台特定机器上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56522771/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com