@Blunker

Что это за ошибка?

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <deque>
#include <map>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <algorithm>
#include <future>

std::mutex lock;

bool sort_word(std::pair<std::size_t, std::string> i, std::pair<std::size_t, std::string> j)
{
    if (i.second < j.second)
        return true;
    else
        return false;
}

void create_pipeline(const std::string&, const std::string&, std::vector<std::pair<std::size_t, std::string>>&);
void split_pipeline(std::vector<std::pair<std::size_t, std::string>>&);
void lower_pipeline(std::vector<std::pair<std::size_t, std::string>>&);
void stat_pipeline(std::vector<std::pair<std::size_t, std::string>>&);
void sort_pipeline(std::vector<std::pair<std::size_t, std::string>>&);
void split_str(std::string&);
void lower_str(std::string&);
void vector_sort(std::vector<std::pair<std::size_t, std::string>>&);

int main()
{
    int n;
    std::string *infiles, *outfiles;
    std::deque<std::thread> pool;
    std::vector<std::pair<std::size_t, std::string>> freq;

    std::cout << "Введите количество файлов\n";
    std::cin >> n;

    infiles = new std::string[n];
    outfiles = new std::string[n];

    std::cout << "Введите имена входных файлов\n";

    for (std::size_t i = 0; i < n; i++)
        std::cin >> infiles[i];

    std::cout << "Введите имена выходных файлов\n";

    for (std::size_t i = 0; i < n; i++)
        std::cin >> outfiles[i];

    for (std::size_t i = 0; i < n; i++)
        pool.push_back(std::thread(create_pipeline, std::ref(infiles), std::ref(outfiles), std::ref(freq)));

    while (pool.size())
    {
        pool.front().join();
        pool.pop_front();
    }

    return 0;
}

void create_pipeline(const std::string &infile, const std::string &outfile, std::vector<std::pair<std::size_t, std::string>> &freq)
{
    std::ifstream in(infile.c_str());
    std::ofstream out(outfile.c_str());

    /*Считывание теста из файла*/

    while (!in.eof())
    {
        std::string temp;

        std::getline(in, temp);
        {
            std::unique_lock<std::mutex> guard(lock);
            freq.push_back(std::make_pair(0, temp));
        }
    }

    //Стадии конвеера
    std::thread split(std::thread(split_pipeline, std::ref(freq)));
    std::thread lower(std::thread(lower_pipeline, std::ref(freq)));
    std::thread stat(std::thread(stat_pipeline, std::ref(freq)));
    std::thread sort(std::thread(sort_pipeline, std::ref(freq)));

    split.join();
    lower.join();
    stat.join();
    sort.join();

    for (std::size_t i = 0; i < freq.size(); ++i)
        out << freq[i].second << " ";
}

void split_pipeline(std::vector<std::pair<std::size_t, std::string>> &freq)
{
    for (std::size_t i = 0; i < freq.size(); ++i)
    {
        split_str(freq[i].second);
    }
}


void split_str(std::string &str)
{
    for (std::size_t i = 0; i < str.size(); ++i)
    {
        if ((str[i] == '.') || (str[i] == ',') || (str[i] == ';') || (str[i] == ':') || (str[i] == '!') || (str[i] == '?'))
        {
            str[i] = ' ';
        }
    }
}

void lower_pipeline(std::vector<std::pair<std::size_t, std::string>> &freq)
{
    for (std::size_t i = 0; i < freq.size(); ++i)
    {
        lower_str(freq[i].second);
    }
}

void lower_str(std::string &str)
{
    std::for_each(str.begin(), str.end(), ::tolower);
}

void vector_sort(std::vector<std::pair<std::size_t, std::string>> &freq)
{
    std::size_t tmp;

    for (std::size_t i = 0; i < freq.size(); ++i)
    {
        int pos = i;
        tmp = freq[i].first;

        for (std::size_t j = i + 1; j < freq.size(); ++j)
        {
            if (freq[i].first < tmp)
            {
                pos = j;
                tmp = freq[i].first;
            }
        }
        freq[pos].first = freq[i].first;
        freq[i].first = tmp;
    }
}

void sort_pipeline(std::vector<std::pair<std::size_t, std::string>> &freq)
{
    std::sort(freq.begin(), freq.end(), sort_word);
}

void stat_pipeline(std::vector<std::pair<std::size_t, std::string>> &freq)
{
    std::map<std::string, std::size_t> temp;
    std::vector<std::pair<std::size_t, std::string>>::iterator iter;
    int j = 0;

    for (iter = freq.begin(); iter != freq.end(); ++iter)
    {
        temp[iter->second]++;
    }

    std::map<std::string, std::size_t>::iterator i;
    freq.clear();

    for (j = 0, i = temp.begin(); i != temp.end(); ++i,j++)
    {
        freq[j].first = i->second;
        freq[j].second = i->first;
    }
}


При компиляции этого кода, компилятор пишет не понятные ошибки:
In file included from /usr/include/c++/4.9/thread:39:0,
                 from main.cpp:8:
/usr/include/c++/4.9/functional: In instantiation of ‘struct std::_Bind_simple<void (*(std::reference_wrapper<std::basic_string<char>*>, std::reference_wrapper<std::basic_string<char>*>, std::reference_wrapper<std::vector<std::pair<long unsigned int, std::basic_string<char> > > >))(const std::basic_string<char>&, const std::basic_string<char>&, std::vector<std::pair<long unsigned int, std::basic_string<char> > >&)>’:
/usr/include/c++/4.9/thread:140:47:   required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (&)(const std::basic_string<char>&, const std::basic_string<char>&, std::vector<std::pair<long unsigned int, std::basic_string<char> > >&); _Args = {std::reference_wrapper<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*>, std::reference_wrapper<std::basic_string<char, std::char_traits<char>, std::allocator<char> >*>, std::reference_wrapper<std::vector<std::pair<long unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<long unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > >}]’
main.cpp:57:106:   required from here
/usr/include/c++/4.9/functional:1665:61: error: no type named ‘type’ in ‘class std::result_of<void (*(std::reference_wrapper<std::basic_string<char>*>, std::reference_wrapper<std::basic_string<char>*>, std::reference_wrapper<std::vector<std::pair<long unsigned int, std::basic_string<char> > > >))(const std::basic_string<char>&, const std::basic_string<char>&, std::vector<std::pair<long unsigned int, std::basic_string<char> > >&)>’
       typedef typename result_of<_Callable(_Args...)>::type result_type;
                                                             ^
/usr/include/c++/4.9/functional:1695:9: error: no type named ‘type’ in ‘class std::result_of<void (*(std::reference_wrapper<std::basic_string<char>*>, std::reference_wrapper<std::basic_string<char>*>, std::reference_wrapper<std::vector<std::pair<long unsigned int, std::basic_string<char> > > >))(const std::basic_string<char>&, const std::basic_string<char>&, std::vector<std::pair<long unsigned int, std::basic_string<char> > >&)>’
         _M_invoke(_Index_tuple<_Indices...>)


Как это можно исправить?
  • Вопрос задан
  • 530 просмотров
Пригласить эксперта
Ответы на вопрос 1
AxisPod
@AxisPod
Каша, а не код.

pool.push_back(std::thread(create_pipeline, std::ref(infiles), std::ref(outfiles), std::ref(freq)));

Зачем тут std::ref для infiles и outfiles. Если в функции они константные.

В функции аргументы всегда помечайте как const&, разве что за исключением случаев передачи примитивных типов и неконстантных или универсальных ссылок. А иначе постоянные копирования данных не есть хорошо.

std::thread split(std::thread(split_pipeline, std::ref(freq))); тут вообще чего хотели добиться? Зачем вызов копирующего конструктора? Чем не устраивает std::thread split(split_pipeline, std::ref(freq)); ?

Покройте типы std::vector и std::map с помощью typedef ну или альясы сделайте.
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы