简体   繁体   中英

Calling member function of a template class recursively

I have a working implementation of a avltree as a template class. I am adding two functions to this working implementation. These two functions that will transverse through the entire tree recursively and preform some calculations.

//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;

When I try calling this function recursively, I receive this error message:

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 );
                           ^

Why are their ampersands on the argument types to the recursive call? Are these references, and if so - how am I doing this?

You forgot to pass count in the recursive calls.

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

The signature looks like

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

But your call looks like

transverseTree( t->right, word, matchingWords );

I think you forgot to pass the count pointer.

It probably has to do with your recursive calls not having the correct parameters.

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

Here, when you declare this function, it takes in 4 parameters.

However, when you call this function recursively:

transverseTree( t->left, word, matchingWords );

You're forgetting about that last parameter *count , therefore that function you're trying to call is not defined with that particular function signature.

Ampersands don't matter here; they just allow passing an argument as a reference. Still, functions having non-reference arguments of the same type will also match (effectively requiring object copying before the function call) provided that there is a copy constructor defined (either explicitly or by default) for an argument type. In this case, the object type is a pointer, and a copy constructor is implicitly defined for it (merely copying the value). So there is no problem with that.

Still it seems the last argument, count , is missing in the recursive calls. It may be the cause of a compilation error (of course unless your have specified a default value for it in a declaration inside AvlTree class).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM