Solved

C++: Need help with binary tree

Posted on 2003-12-10
4
878 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Using Delphi code in a Builder project that uses a DLL 3 114
TTimer no longer functioning 7 91
What does std::atomic give me? 7 124
C++ mouse_event mouse look 7 83
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

773 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