【发布时间】:2012-07-08 09:14:58
【问题描述】:
我用的是自定义的Set类,和STL Set类基本一样
问题是我以某种方式错误地实现了它,并且使用了默认比较函数而不是我定义的比较。
Set<Lexicon::CorrectionT> Lexicon::suggestCorrections(Lexicon::MatchesT & matchSet)
{
Set<CorrectionT> suggest(compareCorr); //ordered Set
suggestCorrectionsHelper(root, suggest, 0, matchSet.testWord, 0, matchSet.testTime);
return suggest;
}
int compareCorr(Lexicon::CorrectionT a, Lexicon::CorrectionT b)
{
if (a.editDistance < b.editDistance)
return -1;
else if (a.editDistance == b.editDistance)
return 0;
else
return 1;
}
struct CorrectionT {
int editDistance; //stackItems
string suggestedWord; //stackItems
};
一些研究:
- The class library
- the same class 的问题 - 建议使用 STL Set 类,在这种情况下,我可能仍需要帮助了解如何定义比较函数,因此发布问题
我有 19 个 C2784 错误都与某种形式的“错误 C2784: 'bool std::operator ==(const std::_Tree<_traits> &,const std::_Tree<_traits> &)' 相关:可以不从 'Lexicon::CorrectionT' 中推断出 'const std::_Tree<_traits> &' 的模板参数
并引用此代码
template <typename Type>
int OperatorCmp(Type one, Type two) {
if (one == two) return 0;
if (one < two) return -1;
return 1;
}
我的问题:你建议如何纠正这个问题?
尝试更改默认定义类型:
#include "lexicon.h"
//template <typename Type>
int OperatorCmp(Lexicon::CorrectionT one, Lexicon::CorrectionT two) {
if (one.editDistance == two.editDistance) return 0;
if (one.editDistance < two.editDistance) return -1;
return 1;
}
#endif
【问题讨论】:
标签: c++ visual-studio-2008 set