2015-10-26 32 views
1

我試圖做幾個線程來計算平均,最小,最大和標準偏差在同一時間。Extrange錯誤時,通過一個變量作爲參考線程在C++

這樣的(T是100尺寸的雙陣列):

P[0] = thread(&average, T, ref(average)); 
P[0].join(); 
P[1] = thread(&maxmin, T, ref(max), ref(min)), 
P[2] = thread(&standardDev, T, ref(stdDev),ref(average)); 

P[1].join(); 
P[2].join(); 

而且在該方法中:

g++ -pthread -std=c++11 (filename) -o (name) 

但:

void average(double* T, double &average) { 
double sum=0.0; 
for(int i=0;i<100;i++){ 
    sum +=T[i]; 
} 
average = sum/100; 
} 

然後我與編譯它失敗並顯示此錯誤:

In file included from /usr/include/c++/4.8/thread:39:0, 
      from ejercicio_4.cpp:2: 
/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple<double*(double*, std::reference_wrapper<double>)>’: 
/usr/include/c++/4.8/thread:137:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = double*; _Args = {double (&)[100], std::reference_wrapper<double>}]’ 
ejercicio_4.cpp:89:40: required from here 
/usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<double*(double*, std::reference_wrapper<double>)>’ 
     typedef typename result_of<_Callable(_Args...)>::type result_type; 
                  ^
/usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<double*(double*, std::reference_wrapper<double>)>’ 
     _M_invoke(_Index_tuple<_Indices...>) 
     ^

我有g ++ 4.8,所以它應該與線程一起工作,我不知道是什麼導致了這個錯誤。

在此先感謝。

+0

你能提供一個最小的測試用例嗎? –

回答

2

在第一行有一個拼寫錯誤或名稱衝突,您在第二個參數中傳遞函數的名稱(average)而不是目標變量的名稱。你可能想寫的是:

double avg; 
P[0] = thread(&average, T, ref(avg)); 
+2

也許這不是一個錯字,但名稱衝突 – Slava

+0

我已經更新了我的答案,以解釋這種可能性thx! –

+0

WOAH!就是這樣!我不能相信我錯過了,我確信這是一個參考錯誤,我沒有意識到,謝謝! – dari1495