Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Problem matching Function Signitures with const

Posted on 2004-10-29
2
Medium Priority
?
461 Views
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;  

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

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

                         

0
Comment
Question by:travishaberman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 3

Accepted Solution

by:
georg74 earned 2000 total points
ID: 12448484

> 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,
georg
0
 

Author Comment

by:travishaberman
ID: 12454056
Thank you much George!
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

618 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