I'm having a little trouble with a critical function in my program, I've put bug testing lines everywhere and singled it down to a single if statement"
template <typename Item>
bool BTNode<Item>::isNull(string leftOrRight)
{
std::cout<<"Is NULL test outer."<<endl;
bool returnNullTest = true;
if (leftOrRight == "left")
{
std::cout<<"Is NULL test inner 1."<<endl;
if (left != NULL)
{
returnNullTest = false;
}
}
else if (leftOrRight == "right") //.c_str()
{
std::cout<<"Is NULL test inner 2."<<endl;
if (right != NULL)
{
returnNullTest = false;
}
}
std::cout<<"NULL TEST FINISHED."<<endl;
return returnNullTest;
}
this is the output:
Is NULL test outer.
Is NULL test inner 2.
Segmentation fault (core dumped)
this is the definition of 'left' and 'right':
BTNode<Item>* left;
BTNode<Item>* right;
in the constructors for BTNode 'left' and 'right' are defined as:
left = NULL;
right = NULL;
does anyone have an idea as to where i'm going wrong with this, i've tried the line
if (left == NULL)
and
if (right == NULL)
with the Boolean switched around but I got the same error.
this is 'BTNode.h'
#ifndef matt_BTNode
#define matt_BTNode
#include <cstdlib>
namespace mattassign3{
template <typename Item>
class BTNode
{
private:
Item* data;
BTNode<Item>* left;
BTNode<Item>* right;
public:
BTNode();
BTNode(Item* startingData);
~BTNode();
BTNode<Item>* getLeft();
BTNode<Item>* getRight();
Item* getData();
bool isNull(string leftOrRight);
void setLeft(BTNode<Item>* leftToSet);
void setRight(BTNode<Item>* rightToSet);
void printInclData();
float comparableNumber();
string comparableString();
};
}
#include "BTNode.template"
#endif
BTNode?coutstatement printed for reasons which are not important to this discussion. Now would be a good time to learn how to use a debugger: google for gdb.isNullfor a few more hours.