C++ C++;:func模板的实例化有问题
我正在从事一个项目,其中包括我编写的类和函数模板。 编译时出现以下错误:C++ C++;:func模板的实例化有问题,c++,templates,C++,Templates,我正在从事一个项目,其中包括我编写的类和函数模板。 编译时出现以下错误: 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 =
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]':
函数'void KNN(const std::vector&,std::vector&)[with T=int,unsigned int K=5u]中的SNN.h
然后是这些错误(我认为它们是相互关联的):
1)从“void ClusteringExample()[with T=int]实例化”
2) 从此处实例化:ClusteringExample();
3) SNN.h[警告]将浮点与==或!=进行比较不安全
**4) 已请求从“%”转换为非标量类型“\uuu gnu\u cxx::\uu normal\u迭代器**
当ClusteringExampke是func模板时。以下是它的实现:
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 ClusteringExample()
{
std::向量T_输入;
std::向量簇;
FillVector(T_输入,每行计数);
SNN(T_输入,集群);
对于(typename std::vector::size_type i=0;
i表达式距离(所有项[i],所有项[j])==距离[d]
有时可能会产生令人惊讶的效果…您永远不应该比较浮点数(浮点数
或双精度
)是否相等或不相等
我建议您阅读IEEE 754标准和谷歌双重平等比较,以获得更多信息
此外,比较和注释似乎不是并行不悖的,您正在比较相等,但注释声称:
//every item which is closer then the "farest" is added to the cluster
如果你真的有这样的代码,也许你的意思是:
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)<<" ";
}
}
template void Cluster::Print()常量{
对于(typename vector::const_iterator iter=\u cluster\u vec.begin;iter!=\u cluster\u vec.end();++iter){
可能您只提供了部分错误消息…我在前两条错误消息之后添加了警告消息。您丢失了错误消息本身。请标记主错误消息提供的行。这意味着主错误行未显示在您发布的代码中(至少我无法发现它)??是否SNN
只能用std::vector
类型调用?@david rodriguez dribeas感谢您的回答,但KNN是一个无法更改的给定代码。我相信我的核心问题与错误4或func模板的定义有关。
//every item which is closer then the "farest" is added to the cluster
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)<<" ";
}
}