-2

When calling Node Counting function in public I get incompatible declaration and in private my returns do not match the function and I cannot call in the test driver.

Float, int, void, bool(stupid I know).

CPP

void BinaryTree::count(TreeNode* root) {
if(root == NULL)
    return 0;
else 
    if(root->left == NULL && root->right == NULL)
        return 1;
    else
        return count(root->left) + count(root->right) + 1;    
}

Header (I think I need to make this public so I can call in the test driver but I was only able to declare in the CPP from Private.)

void count(TreeNode *);

Driver

cout << "Total Number of Nodes " << endl;
tree.count();
cout << endl;

In the Driver test CPP tree.count is inaccessible which is understandable because its being called from private, but as a public call the declaration is incompatible.

Tyler Kanz
  • 23
  • 5
  • 1
    `BinaryTree::count` requests a `TreeNode*` as argument but `tree.count();` as no argument. If _TreeNode_ is internal to BinaryTree you need a public operation without arg and a private operation with the arg called by the public version – bruno May 14 '19 at 19:19
  • 2
    `void BinaryTree::count(TreeNode* root) { if(root == NULL) return 0;` - is *obviously* wrong. Your function returns `void` (nothing) but you try to return a value (an integer). May I suggest reading [a good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) (or two).? There are some basics you have not yet grasped. – Jesper Juhl May 14 '19 at 19:20
  • @bruno Im pretty new to classes. I have changed it into an int function and everything seems to be right except the test driver. How would I go about making tree.count into an argument? – Tyler Kanz May 14 '19 at 19:30
  • @TylerKanz please [edit] your question and show how the `BinaryTree` class is defined. It appears that `BinaryTree::count` is probably `private` and so can't be directly called outside the class, but that's only guesswork. – alter_igel May 14 '19 at 19:32

1 Answers1

2
void BinaryTree::count(TreeNode* root) {
  if(root == NULL)
   return 0;
  else 
   if(root->left == NULL && root->right == NULL)
       return 1;
   else
       return count(root->left) + count(root->right) + 1;    
}

that operation return an int (at least a number) , it must have a signature returning a number, for instance

int BinaryTree::count(TreeNode* root) 

your definition is also complicated for nothing, can be

 int BinaryTree::count(TreeNode* root) {
   return (root == NULL)
     ? 0
     : count(root->left) + count(root->right) + 1;    
 }

and because it does not modify the instance make it const

 int BinaryTree::count(TreeNode* root) const {
   return (root == NULL)
     ? 0
     : count(root->left) + count(root->right) + 1;    
 }

Having

tree.count();

without arg and visibly writing the count you need an other operation like

void BinaryTree::count() const {
  cout << count(..the tree node..);
}

that operation must be public, probably the previous one is private

Anyway it is better to not write the count but to return it, letting the caller doing what it wants with.

So finally something like :

// I use a _struct because fields accessible by BinaryTree
// but  may be a class and BinaryTree is a friend class etc
struct TreeNode {
  // ...
  TreeNode * left;
  TreeNode * right;
  // ...
};

class BinaryTree {
  public:
    // ...
    int count() const { return count(tree); }
    // ...
  private:
    // ...
    int count(TreeNode *) const;
    // ...
    TreeNode * tree;
    // ...
};

int BinaryTree::count(TreeNode* root) const {
   return (root == NULL)
     ? 0
     : count(root->left) + count(root->right) + 1;    
 }

and somewhere cout << "Total Number of Nodes " << tree.count() << endl;

bruno
  • 32,421
  • 7
  • 25
  • 37
  • Ah got it! Thank you! – Tyler Kanz May 14 '19 at 19:46
  • @TylerKanz ok, happy coding – bruno May 14 '19 at 19:47
  • @bruno I didn't downwote, but you should seriosly consider to stop answering _off-topic_ questions. These won't have any benefit for future research. I've been watching you doing this frequently in the [tag:c++] tag realm. Rather use your powers to sort out for good questions, and apply your powers to comment, down voting and close voting. Even better search for possible dupes, and flag as such. – πάντα ῥεῖ May 14 '19 at 19:53
  • 1
    @πάνταῥεῖ I know we do not agree all the time, I probably already said you I come here to **help** rather than to *kick ass* (I speak about the OP, not about you of course ^^). For me the question wasn't not *off-topic*, we do not put the level of *off-topicness* at the same level :-) – bruno May 14 '19 at 19:54
  • @bruno _"I come here to help"_ Well you should notice that Stack Overflow isn't meant as a personal help desk. – πάντα ῥεῖ May 14 '19 at 19:56
  • 1
    @TylerKanz No, it wasn't a good question. You've been missing to post a [mcve] research, debugging and a number of other things before asking. – πάντα ῥεῖ May 14 '19 at 19:57
  • 1
    @πάνταῥεῖ Im sorry I offended you and broke the rules. Like Ive said Im newish to this. – Tyler Kanz May 14 '19 at 20:00
  • 1
    @πάνταῥεῖ I am not supporting anybody. The OP lacks a minimal complete solution. However, downvoter of this answer could just show up and warn the bruno to don't do or wait for the MCVC. Anonymous DV's I found really bad. On the other hand, the answer isn't at all bad. – UserUsing May 14 '19 at 20:08
  • @UsingCpp thank you. Often the quality of a question is grey, but is that grey dark or light ? We do not have all the same view. Anyway to reassure πάνταῥεῖ I also vote to close questions ;-) – bruno May 14 '19 at 20:22