Link to home
Start Free TrialLog in
Avatar of travishaberman
travishaberman

asked on

Problem matching Function Signitures with const

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;  

to...

                            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.

THANK YOU ALL!!!


===========================================================================================================
//BTree.h fiel
===========================================================================================================

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

using namespace std;

#ifndef BTREE_H
#define BTREE_H

class BTree
{
public:
      
                //~tree();//
      //int findkey(const int& key) const;//
      //int findmax(const int& key) const;//

      BTree();
      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();

private:

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

};

#endif

===========================================================================================================
===========================================================================================================

//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==NULL)
                  return;
            if(parent->left != NULL)
                  leafcount(parent->left, count);
            if(parent->right != NULL)
                  leafcount(parent->right, count);
            else
                  count = count + 1;
}

===========================================================================================================
===========================================================================================================

                         

ASKER CERTIFIED SOLUTION
Avatar of georg74
georg74

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of travishaberman
travishaberman

ASKER

Thank you much George!