Solved

C++: Need help with binary tree

Posted on 2003-12-10
4
884 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall‚Ķ
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 clear a vector as well as how to detect empty vectors in C++.

707 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