Declaring structs/classes as private with a class

Nov 15, 2019 at 4:03am
Problem: At various points in working with linked lists, stacks, queues and trees, I have tried to declare the Node struct as a private member of the tree (in this case) class. Everything I can find says that there shouldn't be any issue with this, and it should work the same as any other private member function or variable. When I try to do this, I inevitably get errors from the compiler that read:

tree.cpp:124:28: error: 'struct Tree::Node' is private within this context
void Tree::insert(int val, Node* node)

Am I missing some fine point when it comes to declaring structs as private members of a class? There isn't any particular reason why I can't declare them as public members, but I would like to understand the issue I'm running into.

Header file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#ifndef TREE_H
#define TREE_H


#include<vector>

class Tree
{
     private:          
          struct Node
          {
               int data;
               Node* llink;
               Node* rlink;

               Node(const int& val)
               {
                    data = val;
                    rlink = NULL;
                    llink = NULL;
               } 
          };
          std::vector<int> contents;
          
          void insert(Node*, int);
          Node* search(Node*, int);
          void clear(Node*);
          int sum(Node*);
          Node* root; 
          void print(Node*);
     public:
          Tree();
          ~Tree();
          void insert(int);
          bool search(int);
          bool isempty();
          void clear();
          void sum();
          int max();
          int min();
          int count();   
          void print();
       

};
#endif 


Insert function throwing the error:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void Tree::insert(Node* node, int val)
{
     if (node == NULL)
     {
          if (val < node->data)
          {
               node->llink = new Node(val);
          }
          else if (val >= node->data)
          {
               node->rlink = new Node(val);
          }
     }
     else
     {
          if (val < node->data)
          {
               insert(val, node->llink);
          }
          else
          {
               insert(val, node->rlink);
          }
     }
     return;
}


Note: I'm still in the middle of debugging this particular project; I'm sure that there are problems with this insert function beyond the specific question I have. Any constructive feedback beyond the scope of my query is warmly welcomed!
Nov 15, 2019 at 7:15am
Your error message says
insert(val,node)

Your code says
insert(node,val)
in its declaration line, but then proceeds to use
insert(val,node)
in its recursion.

Are you looking at/posting the correct code?



On another note, you test if node==NULL ... then proceed to use it when it is!
Last edited on Nov 15, 2019 at 11:47am
Nov 15, 2019 at 9:18am
If Node is private to a class, then it will be inaccessible outside that class. In any outside-class definition of a member function return types would have to be qualified as Tree::Node rather than just Node. I don't know why that doesn't apply to their argument list as well.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;

//==========================================================

class Tree
{
private:          
   struct Node
   {
      int data;
      Node* llink = nullptr;
      Node* rlink = nullptr;

      Node( int val ) : data( val ) {}
   };

   Node* root = nullptr;
   Node* insert( Node*, int );
   void print( Node* );

public:
   void insert( int val ) { root = insert( root, val ); }
   void print() { print( root ); }
};

//----------------------------

Tree::Node* Tree::insert( Node *node, int val )
{
   if ( !node ) return new Node( val );
   if ( val < node->data ) node->llink = insert( node->llink, val );
   else                    node->rlink = insert( node->rlink, val );
   return node;
}

//----------------------------

void Tree::print( Node* node )
{
   if ( !node ) return;
   print( node->llink );
   cout << node->data << " ";
   print( node->rlink );
}

//==========================================================

int main()
{
   Tree tree;
   for ( int i : { 20, 2, -1, 6, 4, 2 } ) tree.insert( i );
   tree.print();
}


-1 2 2 4 6 20 
Last edited on Nov 15, 2019 at 11:44am
Topic archived. No new replies allowed.