Solved

C++: Need help with binary tree

Posted on 2003-12-10
4
874 Views
Last Modified: 2012-06-27
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
Comment
Question by:Ryanbhl
  • 2
  • 2
4 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 9918840
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
 

Author Comment

by:Ryanbhl
ID: 9921737
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
 
LVL 13

Accepted Solution

by:
SteH earned 50 total points
ID: 9926689
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
 

Author Comment

by:Ryanbhl
ID: 9930018
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

943 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

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now