theakst
asked on
initializing a 'root' pointer to non NULL value - BST
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?
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?
ASKER
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...
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.
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.
ASKER
Is there another (simpler) way of setting a pointer of type object to a non NULL value?
>>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.
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.
>>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.
Please show us your class declaration.
Try the following:
base = new Node(classPtr);
Do not include the Node local variable declaration.
ASKER
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?
>> 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?
>>which class declaration do you need?
class DB
class DB
example
class DB
{
public:
DB();
private:
Node *base;
};
class DB
{
public:
DB();
private:
Node *base;
};
ASKER
#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)
#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)
ASKER
#include "Node.h" instead of "treeNode.h"
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.
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.
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.
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.
ASKER
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.
ASKER
When de-referenced, it also has to point to a non-NULL value
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Try above code first
ASKER
Works Beautifully!!
Axter, you're an artist, par excellence!!
Axter, you're an artist, par excellence!!
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.