c++ - 编译器错误出现在一台特定的机器上
问题描述
我有一个基于 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();
}
}
};
解决方案
threads.push_back(std::thread(¶llel_executor<T>::upsweep, this, std::ref(input_vector), thread_partition_up[i], thread_partition_up[i+1], 1));
// ^^^^^^^^^^^^^^^^^^^^^^^
如果您的代码被 MinGW 接受,那就是实现错误。
推荐阅读
- android - android可以成为USB设备的主机吗?
- swift - 按日期搜索笔记的模型
- php - lighthouse php 使用 eloquent 模型访问器和修改器
- excel - 从 Spring Boot 应用程序执行 VB 脚本
- node.js - 出现错误:“npm ERR!缺少脚本:开始”,但启动脚本在那里
- c++ - 如何在接收变量值作为输入时使用变量声明数组?检查我的代码
- javascript - 尝试使用正则表达式显示无效电子邮件的错误,但更改 reactjs 后无法提交
- javascript - jQuery对象中的反向迭代?
- node.js - [SyntaxError: JSON Parse error: Unrecognized token '<']
- html - 每当我放大或调整窗口大小时,HTML 导航栏都会消失并在页面上移动