我的一個項目,其中包括類和函數模板,我寫了工作的實例化的問題。 當編譯我遇到這個錯誤: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,雙和類類型)。 這個錯誤對我沒有太多的信息。 任何人都有線索問題可以在哪裏?
你只提供錯誤信息的一部分...... –
我添加了一個警告,按摩這是第一次兩個錯誤消息後。 – user1476869
你錯過了錯誤信息本身。 – Mat