Alright, so I am designing a program that contains binary trees to function as a parser. I have encounted a very strange problem that is causing my program to fail while it is running. I have done several tests and narrowed down the incidents.
Ok, so this results in a successful test:
#include <iostream>
#include <cstdlib>
#include "BinaryTree.cpp"
#include "Stack.cpp"
using namespace std;
int main()
{
BinaryTree<char> treeL, treeR;
Stack< BinaryTree<char> > stack1;
BinaryTree<char> tree1('a');
cout << "Test";
return 0;
}
Output:
Test
RUN SUCCESSFUL (total time: 93ms)
Now, whenever I throw in the push function (the method that causes the program to crash the most):
#include <iostream>
#include <cstdlib>
#include "BinaryTree.cpp"
#include "Stack.cpp"
using namespace std;
int main()
{
BinaryTree<char> treeL, treeR;
Stack< BinaryTree<char> > stack1;
BinaryTree<char> tree1('a');
cout << endl << endl << endl << "Test";
stack1.push(tree1);
return 0;
}
I get this output (periods added to show newlines):
.
.
.
RUN FAILED (exit value 1, total time: 271ms)
Notice how it still prints the newlines but fails to print the text. Even stranger is that the push function comes after the cout, it shouldn't affect it.
Lastly, whenever I do the pop function instead:
#include <iostream>
#include <cstdlib>
#include "BinaryTree.cpp"
#include "Stack.cpp"
using namespace std;
int main()
{
BinaryTree<char> treeL, treeR;
Stack< BinaryTree<char> > stack1;
BinaryTree<char> tree1('a');
cout << endl << endl<< endl << "Test";
stack1.pop();
return 0;
}
I get this:
.
.
.
Test
The stack is already empty.
RUN FAILED (exit value 1, total time: 168ms)
With that method, it prints the text correctly and completes the method, but fails immediately after. I can't for the life of me understand why it does these things but maybe you can help. Here is my push and pop functions:
template <class elemType>
void Stack<elemType>::push(elemType x)
{
Stack_Node<elemType> *newNode;
newNode = new Stack_Node<elemType>;
newNode->value = x;
newNode->link = stackTop;
stackTop = newNode;
}
template <class elemType>
elemType Stack<elemType>::pop()
{
Stack_Node<elemType> *temp;
if (!IsEmpty())
{
temp = stackTop;
stackTop = stackTop->link;
return temp->value; //Returns the value of the top node.
delete temp; //Deletes the top node in the stack.
}
else
cout << endl << "The stack is already empty." << endl;
}
here is the BinaryTree constructor being used:
//Constructor #2
template<class Node_Type>
BinaryTree<Node_Type>::BinaryTree(Node_Type nodeValue)
{
root = new Tree_Node;
root->Node_Info = nodeValue; //Store value into root node.
root->left = NULL;
root->right = NULL;
}
and stack.h:
#ifndef STACK_H
#define STACK_H
//Node definition.
template<class elemType>
struct Stack_Node
{
elemType value;
Stack_Node<elemType> *link;
};
template<class elemType>
class Stack
{
public:
const Stack<elemType>& operator=(const Stack<elemType>&);
//Overloaded assignment operator.
void initialize();
//Function to initialize the stack.
//Postcondition: stack is empty.
void push(elemType x);
//Function to add x to the stack.
//Precondition: The stack exists and is not full.
//Postcondition: x is added to the top of the stack.
elemType pop();
//Function to return and remove the top element of the stack.
//Precondition: The stack exists and is not empty.
//Postcondition: The top element is returned then removed from the stack
bool IsEmpty() const;
//Function to determine if the stack is empty.
//Postcondition: Returns true if stack is empty, else returns
// false.
Stack();
//Default constructor.
//Postcondition: The top of the stack is set to NULL.
Stack(const Stack<elemType>& otherStack);
//Copy constructor.
~Stack();
//Destructor.
//Removes all elements from the stack.
//Postcondition: The list is deleted.
private:
Stack_Node<elemType> *stackTop;
//Pointer to the top of the stack.
void copyStack(const Stack<elemType>& otherStack);
//Function to make a copy of otherStack.
//Postcondition: A copy of otherStack is made and assigned to this one.
};
#endif /* STACK_H */
The IsEmpty function works fine. If any of you need any of the BinaryTree.h , BinaryTree.cpp , or operation overloading code let me know. I'm trying to keep this post as clean as possible. Thanks.