• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 891
  • Last Modified:

C++: Need help with binary tree

Hi experts,

I have the code as following. When i compile it, there were some errors. I dont know how to solve it. Can some please give me a hand on this.

**********Code******************
**********************************
#include <iostream>
#include <conio.h>
#include <fstream>
#include <string>
#include "d_stree.h"
#include "d_tnodel.h"

using namespace std;

// inorder function definition
//
void inorderOutput(stnode<char> *t, const string& seperator=" ")
{
      if(t != NULL )
      {
            inorderOutput(t->left, seperator);
            cout << t->nodeValue << seperator;
            inorderOutput(t->right, seperator);
      }
}

// preorder function definition
//
void preorderOutput(stnode<char> *t, const string& seperator=" ")
{
      
      if(t != NULL)
      {
            cout << t->nodeValue << seperator;
            preorderOutput(t->left, seperator);
            preorderOutput(t->right, seperator);
      }
}

// postorder function definition
//
void postorderOutput(stnode<char> *t, const string& seperator = " ")
{
      if( t != NULL )
      {
            postorderOutput(t->left, seperator);
            postorderOutput(t->right, seperator);
            cout << t->nodeValue << seperator;
      }
}

// main function definition
//
int main()
{
      string str1, str2;
      int size, i=0;
      char *str;


      ofstream outStringFile("string.txt", ios::out);

      if (!outStringFile)
      {
            cerr << "File does not exist" << endl;
            exit(1);
      }


      cout << "Please enter two strings: " << "^Z to end input" << endl;

      while (cin >> str1 >> str2)
            outStringFile << str1 << str2;

      outStringFile.close();

      size = str1.size() + str2.size();
      str = new char[size];

      ifstream inStringFile("string.txt", ios::in);
      inStringFile.getline(str, size);
      
      stree<char> bintree;            // declare the binary tree
      stree<char>::iterator iter;
      
      // create the binary tree
      while(i < size)
      {
            bintree.insert(str[i]);
            i++;
      }

      // display the binary tree
      bintree.displayTree(1);

      //______________+++++Print the value of the tree node+++++_______________
      //
      // print the value of the tree node in Inorder
      cout << "Tree in Inorder:";
      inorderOutput(bintree.root);
      cout << endl;

      // print the value of the tree node in Preorder
      cout << "Tree in Preorder: ";
      preorderOutput(bintree.root);
      cout << endl;

      // print the value of the tree node in Postorder
      cout << "Tree in Postoder: ";
      postorderOutput(bintree.root);
      cout << endl;

      return 0;
}
****************************************************

***********ERRORS OCCURRED**************************
****************************************************
--------------------Configuration: ass5 - Win32 Debug--------------------
--------------------Configuration: ass5 - Win32 Debug--------------------
Compiling...
Assign5.cpp
C:\Documents and Settings\Ryan\My Documents\HomwW\ass5\Assign5.cpp(133) : error C2248: 'root' : cannot access private member declared in class 'stree<char>'
        c:\documents and settings\ryan\my documents\homww\ass5\d_stree.h(132) : see declaration of 'root'
C:\Documents and Settings\Ryan\My Documents\HomwW\ass5\Assign5.cpp(138) : error C2248: 'root' : cannot access private member declared in class 'stree<char>'
        c:\documents and settings\ryan\my documents\homww\ass5\d_stree.h(132) : see declaration of 'root'
C:\Documents and Settings\Ryan\My Documents\HomwW\ass5\Assign5.cpp(143) : error C2248: 'root' : cannot access private member declared in class 'stree<char>'
        c:\documents and settings\ryan\my documents\homww\ass5\d_stree.h(132) : see declaration of 'root'
Error executing cl.exe.

ass5.exe - 3 error(s), 0 warning(s)
*********************************************************

********************DECLARATION OF ROOT*******************
*********************************************************
Private:
            stnode<T> *root;
                  // pointer to tree root
*********************************************************

Thanks in advance!
Ryan


      


      
0
Ryanbhl
Asked:
Ryanbhl
  • 2
  • 2
1 Solution
 
SteHCommented:
Since root is a private member of ... (you don't show enough for that) you can't access it via
bintree.root
You need a get function for it:
public:
   snode<T>* GetRoot (void) {return root};

private data members can only be accessed from the class itself, ie inside a class member function.
0
 
RyanbhlAuthor Commented:
Thanks Steh for your responding,

I did like you said: modify the punblic class with stnode<T> *gerRoot(void). How, I got the same errors while I am trying to compile the code.

And,
**********
Private:
          stnode<T> *root;
               // pointer to tree root
************

It belongs to the template library "d_stree.h" .

0
 
SteHCommented:
in the class declaration (d_stree.h) you need to add the function GetRoot () as public:

public:
      stnode<T> * GetRoot (void) {return root};


and in the code above replace the lines containing
bintree.root
with
bintree.GetRoot ();
ie:
inorderOutput(bintree.GetRoot ());

0
 
RyanbhlAuthor Commented:
Hi SteH,

Thank you so much for your help. After modified the d_stree.h and i made some changes in my .cpp code , the rpogram works fine now.
I really apreciate it!

Ryan=)
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now