这个错误:
错误 C2664: 'Set::Set(int (__cdecl *)(ElemType,ElemType))' : 无法将参数 1 从 'int (__cdecl *)(CorrectionT &,CorrectionT &)' 转换为 'int (__cdecl *)(ElemType ,元素类型)'
是将此比较函数作为基于 BST 的 SET 类的一部分实现的结果,
int compareCorr(struct CorrectionT &a, struct CorrectionT &b)
{
if (a.editDistance < b.editDistance) return -1;
else if (a.editDistance == b.editDistance) return 0;
else return 1;
}
类集
Set(int (*cmpFn)(ElemType, ElemType) = OperatorCmp);
而Set中比较函数的使用是为了添加
template <typename ElemType>
void Set<ElemType>::add(ElemType element) {
bst.add(element);
}
并添加 bst 类重述头文件
BST(int (*cmpFn)(ElemType one, ElemType two) = OperatorCmp);
和功能添加
template <typename ElemType>
bool BST<ElemType>::add(ElemType data) {
bool createdNewNode = false;
recAddNode(root, data, createdNewNode);
if (createdNewNode) timestamp++;
return createdNewNode;
}
template <typename ElemType>
bool BST<ElemType>::recAddNode(nodeT * & t, ElemType & data,
bool & createdNewNode) {
if (t == NULL) {
t = new nodeT;
t->data = data;
t->bf = BST_IN_BALANCE;
t->left = t->right = NULL;
createdNewNode = true;
numNodes++;
return true;
}
int sign = cmpFn(data, t->data);
if (sign == 0) {
t->data = data;
createdNewNode = false;
return false;
}
int bfDelta = 0;
if (sign < 0) {
if (recAddNode(t->left, data, createdNewNode)) {
bfDelta = -1; /* left subtree is higher */
}
} else {
if (recAddNode(t->right, data, createdNewNode)) {
bfDelta = +1; /* right subtree is higher */
}
}
updateBF(t, bfDelta);
return (bfDelta != 0 && t->bf != BST_IN_BALANCE);
}
知道这里发生了什么 - 比较功能有什么问题?