gpt4 book ai didi

c++ - 使用 boost::bind 的调用错误没有匹配函数

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

我正在尝试编写一个并行的冒泡排序函数。我在使用 boost::bind 时遇到错误:

void swap(vector<int>& input, int i, int j)
{
if (input[i] > input[j])
{
int temp = input[j];
input[j] = input[i];
input[i] = temp;
}
return;
}

void parallel_bubblesort(vector<int>& input, int, int)
{

int i, j, temp;

for (i = 0; i < input.size(); i++)
{
boost::asio::io_service ioService;
boost::thread_group threadpool;
for (j = 0; j < input.size() - 1; j++)
{
ioService.post(boost::bind(&swap, boost::ref(input), 2 * j + i % 2, 2 * j + 1 + i % 2));
}

for (int t = 0; t < NUM_THREADS; t++)
{
threadpool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService));
}

threadpool.join_all();
ioService.stop();
}
return;
}

卡在绳子上

ioService.post(boost::bind(&swap, boost::ref(input), 2 * j + i % 2, 2 * j + 1 + i % 2));

错误说

no matching function for call to ‘bind(<unresolved overloaded function type>, std::vector<int, std::allocator<int> >&, int, int)’

这看起来很奇怪,因为这些都没有重载,有人知道这是什么意思吗?

编辑:这是完整的程序

#include <iostream>
#include <chrono>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
#include <cstdlib>
#include <algorithm>
#include <boost/thread.hpp>
#include <boost/asio/io_service.hpp>
#define NUM_THREADS 4

using namespace std;
using namespace boost;
using namespace boost::this_thread;

int check_solution(const vector<int>&);
void bubblesort(vector<int>&);
void swap1(vector<int>&, int, int);
void parallel_bubblesort(vector<int>&);

int main()
{
string line, token;
string file = "test.txt";
vector<int> unsorted_integers;

//print out filename
cout << file << endl;

ifstream myfile(file);

//open file
if(myfile.is_open())
{
//load everything into a vector
while (getline(myfile, line))
{
int temp;
std::istringstream ss(line);
std::getline(ss, token, '\n');
temp = atoi(token.c_str());
unsorted_integers.push_back(temp);
}
}

vector<int> unsorted_integers2(unsorted_integers);
//starts clock
std::chrono::high_resolution_clock::time_point start2 = std::chrono::high_resolution_clock::now();

//run the sort function and check for correctness
parallel_bubblesort(unsorted_integers2);
if(check_solution(unsorted_integers2))
{
cout << "The parallel sort solution is correct" << endl;
}
else
{
cout << "The parallel sort solution is incorrect" << endl;
}

//stops clock and prints times
std::chrono::high_resolution_clock::time_point end2 = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> total_time2 = std::chrono::duration_cast<std::chrono::duration<double>>(end2 - start2);
cout << "The parallel sort function took " << total_time2.count() << " seconds." << endl;

return 0;
}


void swap1(vector<int>& input, int i, int j)
{
if (input[i] > input[j])
{
int temp = input[j];
input[j] = input[i];
input[i] = temp;
}
return;
}

void parallel_bubblesort(vector<int>& input, int, int)
{

int i, j;

for (i = 0; i < input.size(); i++)
{
boost::asio::io_service ioService;
boost::thread_group threadpool;
for (j = 0; j < input.size() - 1; j++)
{
ioService.post(boost::bind(&swap1, boost::ref(input), 2 * j + i % 2, 2 * j + 1 + i % 2));
}

for (int t = 0; t < NUM_THREADS; t++)
{
threadpool.create_thread(boost::bind(&boost::asio::io_service::run, &ioService));
}

threadpool.join_all();
ioService.stop();
}
return;
}

int check_solution(const vector<int>& solution)
{
vector<int> correct_solution(solution);
std::sort(correct_solution.begin(), correct_solution.end());
return (solution == correct_solution);
}

最佳答案

您可能正在某处执行 using namespace std;,而 std::swap 是一回事。我建议您只需将 swap 函数重命名为其他名称即可。

关于c++ - 使用 boost::bind 的调用错误没有匹配函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23509083/

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