Solved

C++: Need help with binary tree

Posted on 2003-12-10
4
870 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

772 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

10 Experts available now in Live!

Get 1:1 Help Now