Problem matching Function Signitures with const

Posted on 2004-10-29
Last Modified: 2008-02-01
Hello Experts,

I have what shold be a fairly easy 500 pts for someone.  I just cant seem to get my function signitures to match correctly in a Tree class that I am making.

My problem is this;

I have a public member function:                

                           int leafcount() const;                      <---  this must be the signiture because it is a requirement of the teacher.

in the function body I place a call to this funciton private member function:

                           void leafcount(const TreeNode*&, int&) const;  

And when I compile I get these errors:

                           BTree.cpp:                  In member function `int BTree::leafcount() const':
                           BTree.cpp:184:            no matching function for call to `BTree::leafcount(TreeNode*const&, int&) const'
                           BTree.cpp:182:            candidates are: int BTree::leafcount() const
                           BTree.h:47:                 void BTree::leafcount(const TreeNode*&, int&) const
                           BTree.cpp:                   In member function `void BTree::leafcount(const TreeNode*&, int&) const':
                           BTree.cpp:                   193: no matching function for call to `BTree::leafcount(TreeNode*const&, int&) const'
                           BTree.cpp:182:             candidates are: int BTree::leafcount() const
                           BTree.cpp:189:             void BTree::leafcount(const TreeNode*&, int&) const
                           BTree.cpp:195:             no matching function for call to `BTree::leafcount(TreeNode*const&, int&) const'
                           BTree.cpp:182:             candidates are: int BTree::leafcount() const
                           BTree.cpp:189:             void BTree::leafcount(const TreeNode*&, int&) const

Now, I can fix all my problems if I simply change my function calls...

                           int leafcount() const;     &     void leafcount(const TreeNode*&, int&) const;  


                            int leafcount()             &      void leafcount(TreeNode*&, int&);

but as I said, I need the const...  So thats it!.. To help you help me, below is my BTree class header file and the implementation of both functions.


//BTree.h fiel

#include <cstdlib>
#include "TreeNode.h"

using namespace std;

#ifndef BTREE_H
#define BTREE_H

class BTree
      //int findkey(const int& key) const;//
      //int findmax(const int& key) const;//

      BTree(const BTree& atree);
      bool insert(const int& d);
      int findheight();
      void RecursiveInOrder(void(*visit)(int& i));
                void NonRecursivePostOrder(void (*visit)(int& i));
      void NonRecursiveLevelOrder(void(*visit)(int& i));
      int nodecount() const;
      int leafcount() const;            
      int countfullnodes();


                //void leafcout(const TreeNode*&, int&);//
      TreeNode *root;
      bool insert(TreeNode*&, const int&);
      bool copyTree(TreeNode* treePtr, TreeNode*& newTreePtr);
      int findheight(TreeNode*);
      void RecursiveInOrder(TreeNode*, void(*visit)(int& i));
      void leafcount(const TreeNode*&, int&) const;
      int totalnodes;
      void countfullnodes(TreeNode*&, int&);




//Implementation of functions:

int BTree::leafcount() const
      int count = 0;
      leafcount(root, count);
      return count;

void BTree::leafcount(const TreeNode*& parent, int& count) const
            if(parent->left != NULL)
                  leafcount(parent->left, count);
            if(parent->right != NULL)
                  leafcount(parent->right, count);
                  count = count + 1;



Question by:travishaberman
    LVL 3

    Accepted Solution


    > void leafcount(const TreeNode*&, int&) const;  

    you wanted to pass reference to a const pointer to the function.
    this is not necessary. chnage it to simply pass the pointer.

    void leafcount(const TreeNode *, int &) const;

    that's all. the way you're calling it is correct.

    if you want to do it in more elegant way, (but still as recursive function,)
    don't pass int & for count, but return the count as the result of the function:

    unsigned leafcount(const TreeNode *node) const
        if (node == 0) return 0;
        return 1 + leafcount(node->left) + leafcount(node->right);

    have fun,

    Author Comment

    Thank you much George!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Suggested Solutions

    Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

    913 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now