jeudi 25 juillet 2019

Erase item from vector passed to new thread C++

I have a program that saves in a vector a reference and the thread id of the newly created thread.

void remote_forwading_thread(std::vector<std::pair<std::thread::id, std::thread>>& thread_vector) {
  [...]
   for (const auto& th : thread_vector) {
        if (th.first == std::this_thread::get_id()) {
            thread_vector.erase(th); <--- ERROR
            break;          
        }
    }
}

std::vector<std::pair<std::thread::id, std::thread>> thread_vector;
[...]
std::thread t(remote_forwading_thread, &thread_vector);
thread_vector.emplace_back(std::make_pair(t.get_id(), std::move(t)));

The errors I'm getting are:

1>C:\Users\user\Documents\code\project\src\client.cpp(214,79): error C2100: illegal indirection 1>C:\Users\user\Documents\code\project\src\client.cpp(220,35): error C2664: 'std::_Vector_iterator>> std::vector<_Ty,std::allocator<_Ty>>::erase(std::_Vector_const_iterator>>,std::_Vector_const_iterator>>)': cannot convert argument 1 from 'const std::pair' to 'std::_Vector_const_iterator>>' 1>C:\Users\user\Documents\code\project\src\client.cpp(220,35): error C2664: with 1>C:\Users\user\Documents\code\project\src\client.cpp(220,35): error C2664: [ 1>C:\Users\user\Documents\code\project\src\client.cpp(220,35): error C2664: _Ty=std::pair 1>C:\Users\user\Documents\code\project\src\client.cpp(220,35): error C2664: ] 1>C:\Users\user\Documents\code\project\src\client.cpp(220,33): message : No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire