gpt4 book ai didi

c++ - 递归调用模板类的成员函数

转载 作者:太空狗 更新时间:2023-10-29 21:39:05 29 4
gpt4 key购买 nike

我有一个 avltree 的工作实现作为模板类。我正在向这个工作实现中添加两个函数。这两个函数将递归地遍历整个树并执行一些计算。

//avltree.cpp
//see comment in code below

template <class Comparable>
void AvlTree<Comparable>::transverseTree( AvlNode<Comparable> *t, const char *word, char matchingWords[100][MAX_LENGTH + 1], int *count) const
{

int distance;

if( t != NULL )
{
distance = levDistance(t->element/*avl word*/, word);
if (distance == 1)
{
*count++;
strcpy(matchingWords[*count], t->element/*avl word*/);
}

//error is here
transverseTree( t->left, word, matchingWords );
transverseTree( t->right, word, matchingWords );
}
}

//avltree.h

//new function
void transverseTree(AvlNode<Comparable> *t, const char *word, char matchingWords[100][MAX_LENGTH + 1],
int *count) const;
//new function
int levDistance(const char *str1, const char *str2) const;

当我尝试递归调用此函数时,收到此错误消息:

AvlTree.cpp:412:31: error: no matching function for call to ‘AvlTree<const char*>::transverseTree(AvlNode<const char*>*&, const char*&, char (*&)[34]) const’
transverseTree( t->left, word, matchingWords );
^

为什么它们的符号在递归调用的参数类型上?这些是引用文献吗?如果是,我该怎么做?

最佳答案

您忘记在递归调用中传递 count

transverseTree( t->left, word, matchingWords, count );  // Missing count
transverseTree( t->right, word, matchingWords, count ); // Missing count

关于c++ - 递归调用模板类的成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33771659/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com