2014-07-10 19 views
-1

我試圖返回一個在加權隨機分佈上返回的向量中的值(即,如果向量有1,2,3,它將給出1/6的機會1和2/6的機會2和3/6的機會3)。我得到編譯時間錯誤vector <double>在C++代碼中出現雙重錯誤

Protein.cpp:809:54: error: cannot convert ‘__gnu_cxx::__normal_iterator<double*, std::vector<double> >’ to ‘double’ in assignment 
Protein.cpp:814:24: error: could not convert ‘((Protein*)this)->Protein::fitness1.std::vector<_Tp, _Alloc>::operator[]<double, std::allocator<double> >((std::vector<double, std::allocator<double> >::size_type)index)’ from ‘double’ to ‘std::vector<double>’ 

當我編譯代碼,並有很少運氣試圖解決它。如果任何人都可以看看這個功能並幫助我,我將不勝感激。再次感謝您的時間!

功能

vector<double> Protein::Init_Seq_Recursive(State& s, int d) 
{ 
    vector<float> sequence; 
    double index; 
    float initial; 
    int i =0; 
    for (i; i < k; i++) 
    { 
     s[d] = i; 
     if (d == (L - 1)) 
     { 
      int ind = Index(s); 


      Ef[ind] = GetEf(s); 
      Eb1[ind] = GetEb1(s); 
      Eb2[ind] = GetEb2(s); 
      double zf = exp(beta_f*Ef[ind]); 
      double zb1 = exp(beta_b*Eb1[ind]); 
      double zb2 = exp(beta_b*Eb2[ind]); 


      fitness1[ind] = (1. + f_ub*zb1 + f_ub*f_uf*zf*zb1)/(1. + zb1 + zf*zb1); 
      fitness2[ind] = (1. + f_ub*zb2 + f_ub*f_uf*zf*zb2)/(1. + zb2 + zf*zb2); 



     }  
     else 
      Init_Fitness_Recursive(s, d + 1); 
    } 
    if(i==k-1){ 
     vector<float> weights; 
     float running_total = 0; 


     for(int y = 0; y<pow(k,L); y++){ 
      running_total = running_total+fitness1[y]; 
      weights.push_back(running_total); 


     } 
     double rnd = (static_cast <double> (rand())/static_cast <double> (RAND_MAX))*running_total; 
     for(int y = 0; y<fitness1.size(); y++){ 
      if(rnd<weights[y]){ 
      index = find(fitness1.begin(), fitness1.end(), rnd); 
      }  
     } 

    } 
    return(fitness1[index]); 

} 
+1

'fitness1'的數據類型是什麼? –

+1

看起來像你正在迴歸一個雙倍的地方,你應該返回一個雙打矢量。 – drescherjm

+1

'fitness1 [index]'可能不是'vector ',該函數應該返回。 –

回答

2

std::find不會返回一個索引。它返回一個指向找到的元素的迭代器。要獲得index,您需要計算迭代器與您找到的迭代器之間的距離。就像這樣:

vector<double>::iterator found = find(fitness1.begin(), fitness1.end(), rnd); 
index = distance(fitness1.begin(), found); 

這是很可疑的,你會使用double爲索引到一個容器中,雖然。

此外,您的return(fitness1[index]);正試圖返回單個double,但返回類型爲vector<double>

2
index = find(fitness1.begin(), fitness1.end(), rnd); 

返回值find是一個迭代器。您需要取消對它的引用獲得double(即是index類型):

index = *find(fitness1.begin(), fitness1.end(), rnd); 
// ^

其次,你想從該聲明爲函數返回一個double返回一個vector<double>

return(fitness1[index]); 
//  ^^^^^^^^^^^^^^^ 
+0

我認爲'index'這個名字建議他想要找到找到的元素的索引,而你認爲​​'double'表明他想要元素本身。有趣。 –

+0

@JosephMansfield你的猜測和我的一樣好(可能更好,因爲我沒有深入研究邏輯)。 – jrok

相關問題