Di seguito è solo un semplice programma per testare utilizzando due thread per inserire una tabella hash. viene utilizzato.C++: errore: nessun tipo denominato 'tipo' in 'classe std :: result_of <void (* (std :: unordered_map
#include <iostream>
#include <unordered_map>
#include <thread>
using namespace std;
void thread_add(unordered_map<int, int>& ht, int from, int to)
{
for(int i = from; i <= to; ++i)
ht.insert(unordered_map<int, int>::value_type(i, 0));
}
void test()
{
unordered_map<int, int> ht;
thread t[2];
t[0] = thread(thread_add, ht, 0, 9);
t[1] = thread(thread_add, ht, 10, 19);
t[0].join();
t[1].join();
std::cout << "size: " << ht.size() << std::endl;
}
int main()
{
test();
return 0;
}
Tuttavia, ci sono errori durante la compilazione di esso.
$ g++ -std=c++11 -pthread test.cpp
...
/usr/include/c++/4.8.2/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<void (*(std::unordered_map<int, int>, int, int))(std::unordered_map<int, int>&, int, int)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
...
voluto un po ', ma ancora non è possibile correggerlo. Grazie.
possibile duplicato di: http://stackoverflow.com/questions/8299545/passing-arguments-to-thread-function correlate : http://stackoverflow.com/questions/5116756/difference-between-pointer-and-reference-as-thread-parameter –