?
Solved

initializing a 'root' pointer to non NULL value - BST

Posted on 2005-03-01
18
Medium Priority
?
306 Views
Last Modified: 2010-04-01
Hi All,
I'm trying to build a database using a Binary Search Tree.  However, I can't get the pointer to root to initialize to a non-NULL value.

The following are the constructors for my Node file:

Node::Node()
{
      class *ptr1 = new class;
      ptr1 = NULL;
      ptr2 = NULL;      
}  

Node::Node( class* classPtr )
{
      class *ptr1 = new class;
      ptr1 = classPtr;
      ptr2 = NULL;      
}  

My Database constructor looks as follows:

DB::DB()            //Creates an empty database
{
      //create a new pointer to the root of the BST
      class *classPtr = new class();
      classPtr ->setMember(1);
      classPtr->setMember(2);
      classPtr->setMemberFn(1);
      Node *root = new Node(classPtr);
}

When I try to gdb the program, the pointer  "root" still shows up as NULL, why is this?
0
Comment
Question by:theakst
[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
  • 9
  • 8
18 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 13435483
>>When I try to gdb the program, the pointer  "root" still shows up as NULL, why is this?

How are you evaluating pointer root?
Since it's the last line in your function, it's probably going to be NULL when you evaluated.

Trying putting an extra dummy line after that line, and then put the breakpoint there.
Then you should be able to evaluate root, and I'm sure you'll find that it's not NULL.
0
 

Author Comment

by:theakst
ID: 13435542

root gets evaluated later by calling one of its member fn's and thusly creating a 'segmentation fault'

I added the following:
.
.
Node *base = new Node(classPtr);
root = base;

<breakpoint>
}

sadly, root still evaluates to 0...
0
 
LVL 15

Expert Comment

by:efn
ID: 13435563
You probably want root to be a member of DB.  As shown, it's just a local variable in the constructor.

You seem to have a class named "class".  Since "class" is a language keyword, this is likely to confuse the compiler.  You should choose a different name for your class.
0
Industry Leaders: 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!

 

Author Comment

by:theakst
ID: 13435564
Is there another (simpler) way of setting a pointer of type object to a non NULL value?

0
 
LVL 30

Expert Comment

by:Axter
ID: 13435565
>>sadly, root still evaluates to 0...

Did you check if Base is evaluated to zero?


>>root gets evaluated later by calling one of its member fn's and thusly creating a 'segmentation fault'

You can not have root be evaluated later.
It goes out of scope at the end of that function.

So what ever you're evaluating is not valid.
0
 
LVL 30

Expert Comment

by:Axter
ID: 13435574
>>Is there another (simpler) way of setting a pointer of type object to a non NULL value?

Please show us your class declaration.

Try the following:
base = new Node(classPtr);

Do not include the Node local variable declaration.
0
 

Author Comment

by:theakst
ID: 13435601
Ok,

>> You probably want root to be a member of DB.  As shown, it's just a local variable in the constructor.


how do I make root to be a member of DB and not just a local var? (sorry for the simple question)


>>Please show us your class declaration.

which class declaration do you need?
0
 
LVL 30

Expert Comment

by:Axter
ID: 13435609
>>which class declaration do you need?

class DB
0
 
LVL 30

Expert Comment

by:Axter
ID: 13435616
example

class DB
{
public:
  DB();
private:
  Node *base;
};
0
 

Author Comment

by:theakst
ID: 13435659
#ifndef _TREEDB_H
#define _TREEDB_H

#include "treeNode.h"

class studentDB {
private:
  Node* root;
  int pCount;
  Node findSmallestRight();

public:
      DB();
      ~DB();

      bool add(class* newRec);
      bool withdraw(unsigned int num, class* searchRecord);
      bool remove(unsigned int num);
      void delete();
      bool Full();
      void print();
}; //DB

#endif

class DB is just the database.  I also have class node & class 'class' (which could (has been)  be renamed record to avoid confusion)
0
 

Author Comment

by:theakst
ID: 13435677

#include "Node.h" instead of "treeNode.h"
0
 
LVL 30

Expert Comment

by:Axter
ID: 13435688
Either you're giving the class the wrong name, or you're giving your constructors and destructors the wrong name.

class studentDB { //You've declared the class name as studentDB

However, you have the following constructor and destructor.

public:
     DB();
     ~DB();

The constructor and destructor must have the same name as the class name.
0
 
LVL 30

Expert Comment

by:Axter
ID: 13435694
class studentDB {
private:
  Node* root;

In above line, you'll already have root as a member of the class, so all you need to do is NOT to declare the local variable when you use it in your functions.
0
 

Author Comment

by:theakst
ID: 13435713

Ok, but how would I make root to be a dynamically allocated member of DB instead of a local var?

For the database to work, I think 'root' has to point to the first node in the BST.

0
 

Author Comment

by:theakst
ID: 13435717
When de-referenced, it also has to point to a non-NULL value
0
 
LVL 30

Accepted Solution

by:
Axter earned 2000 total points
ID: 13435721
class DB{
private:
  Node* root;
 
In above code, root is a member of the class.

You need to allocate memory to it in your constructor
DB::DB()          //Creates an empty database
{
     //create a new pointer to the root of the BST
     class *classPtr = new class();
     classPtr ->setMember(1);
     classPtr->setMember(2);
     classPtr->setMemberFn(1);
     root = new Node(classPtr); //******* Notice NO Node type declaration behind root  *****
}
0
 
LVL 30

Expert Comment

by:Axter
ID: 13435723
Try above code first
0
 

Author Comment

by:theakst
ID: 13435748
Works Beautifully!!

Axter, you're an artist, par excellence!!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

752 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