2012-06-23 32 views
0

我的一個項目,其中包括類和函數模板,我寫了工作的實例化的問題。 當編譯我遇到這個錯誤:C++:具有與FUNC模板

SNN.h In function `void KNN(const std::vector<T, std::allocator<_CharT> >&, std::vector<Cluster<T, K>, std::allocator<Cluster<T, K> > >&) [with T = int, unsigned int K = 5u]': 

跟此錯誤(我認爲他們是連接):

1)instantiated from `void ClusteringExample() [with T = int]' 
2)instantiated from here:ClusteringExample<int>(); 
3)SNN.h [Warning] comparing floating point with == or != is unsafe 
**4) conversion from `<unknown type>' to non-scalar type `__gnu_cxx::__normal_iterator<const Product*, std::vector<Product, std::allocator<Product> > >' requested** 

當ClusteringExampke是FUNC模板。這裏是它的實現:KNN的

template <typename T> 
void ClusteringExample() 
    { 
     std::vector<T> T_input; 
     std::vector<Cluster<T,cluster_size> > clusters_T; 
     FillVector(T_input, count_per_line); 
     SNN(T_input, clusters_T); 
     for (typename std::vector<Cluster<T,cluster_size> >::size_type i=0;  
    i<clusters_T.size(); i++) 
     { 
      clusters_T[i].Print(); 
      std::cout << std::endl; 
     } 
     std::cout << "===="<< std::endl; 
    } 

代碼: **

void KNN(const std::vector<T>& all_items, std::vector<Cluster<T,K> >& result) 
{ 
    result.clear(); 
    if (K > all_items.size()) return; //we cannot create clusters bigger than the number of all items 
    //for each item in all_items we will create a cluster 
    for (typename std::vector<T>::size_type i=0; i < all_items.size(); i++) 
    { 
     //this vector will save distances to the item i 
     std::vector<double> distances; 
     //passing over all items and calculating distance to the item i 
     for (typename std::vector<T>::size_type j=0; j < all_items.size(); j++) 
     { 
      distances.push_back(Distance(all_items[i], all_items[j])); 
     } 
      //creating new cluster 
     Cluster<T,K> new_cluster; 
     //we are looking for K nearest distances 
     std::sort(distances.begin(), distances.end()); 
     for (std::vector<double>::size_type d=0; d<K; d++) 
     { 
       for (typename std::vector<T>::size_type j=0; j < all_items.size(); j++) 
       { 
        if (Distance(all_items[i], all_items[j]) == distances[d]) //every item which is closer then the "farest" is added to the cluster 
        { 
        new_cluster.Add(all_items[j]); //we don't use the return value of Add in this implementation, but you need to support it 
        } 
       } 
     } 
     result.push_back(new_cluster); 

** 不得不說:這是發生於所有類型我試圖實例化(INT,雙和類類型)。 這個錯誤對我沒有太多的信息。 任何人都有線索問題可以在哪裏?

+0

你只提供錯誤信息的一部分...... –

+0

我添加了一個警告,按摩這是第一次兩個錯誤消息後。 – user1476869

+2

你錯過了錯誤信息本身。 – Mat

回答

0

表達Distance(all_items[i], all_items[j]) == distances[d]可能有意想不到的效果有時......你不應該比較相等或不等的浮點數(floatdouble)。

我建議你閱讀IEEE 754標準和谷歌雙相等比較以獲取更多信息。

另外,比較和評論似乎並沒有用手去手,你是比較平等,但評論聲稱:

//every item which is closer then the "farest" is added to the cluster 

也許你的意思<=

+0

@ david-rodriguez-dribeas謝謝你的回答,但是KNN是一個無法改變的給定代碼。我相信我的核心問題與錯誤號4相關或者定義了func模板。 – user1476869

2

如果真有這樣的代碼:

template<typename T, size_t K> void Cluster<T,K>::Print() const { 
    for(typename vector<T>::const_iterator iter=_cluster_vec.begin;iter!=_cluster_vec.end();++iter) { 
    cout<<(*iter)<<" "; 
    } 
} 

則(a)您需要更改beginbegin()和(b)你會得到一個答案越早如果你發佈的代碼有錯誤在你原來的問題。