使用boost :: bind调用错误没有匹配函数

时间:2014-05-07 05:01:10

标签: c++ boost boost-asio boost-thread

我正在尝试编写并行bubblesort函数。我在使用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);
}

1 个答案:

答案 0 :(得分:2)

你可能正在某处using namespace std;std::swap是一件事。我建议您只需将swap功能重命名为其他功能。