brich744
asked on
Numerous Errors
Hello Everyone,
I am finishing up a program that prints out a conceptual tree of a postfix equation. Every thing was working just fine until I added this function.
The errors that I am receiving are:
All of these errors are coming from that one function that I added. I will post entire program.
BinaryNode.h
StackLi.h
Main.cpp
I am finishing up a program that prints out a conceptual tree of a postfix equation. Every thing was working just fine until I added this function.
template<class object>
void BinaryNode<object>::display_tree(Stack<BinaryNode<char>*> *stack, int size)
{
char array_display_tree = new char[size][size];
while(!stack->isEmpty())
{
char *currentNode(stack->topAndPop());
array_display_tree[currentNode->row_position][currentNode->level] = currentNode->data;
}
}
The errors that I am receiving are:
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\b inarytree\ stackli.h( 62): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\b inarytree\ stackli.h( 62): error C2988: unrecognizable template declaration/definition
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\b inarytree\ stackli.h( 62): error C2059: syntax error : '<'
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\b inarytree\ stackli.h( 70): error C2143: syntax error : missing ';' before '{'
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\b inarytree\ stackli.h( 70): error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\b inarytree\ stackli.h( 77): error C2143: syntax error : missing ';' before '<'
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\b inarytree\ stackli.h( 77): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\b inarytree\ stackli.h( 77): error C2086: 'int Stack' : redefinition
1> c:\users\owner\documents\visual studio 2010\projects\binarytree\b inarytree\ stackli.h( 62) : see declaration of 'Stack'
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\b inarytree\ stackli.h( 77): error C2988: unrecognizable template declaration/definition
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\b inarytree\ stackli.h( 77): error C2059: syntax error : '<'
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\b inarytree\ stackli.h( 85): error C2588: '::~Stack' : illegal global destructor
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\b inarytree\ stackli.h( 85): fatal error C1903: unable to recover from previous error(s); stopping compilation
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
All of these errors are coming from that one function that I added. I will post entire program.
BinaryNode.h
#ifndef BINARYNODE_H
#define BINARYNODE_H
#include<iomanip>
#include<iostream>
#include<queue>
#include<string>
#include<math.h>
using namespace std;
#include"StackLi.h"
template<class object>
class BinaryNode
{
public:
BinaryNode(object&);
BinaryNode(BinaryNode<object> * , int );
void breadth_first_search(BinaryNode<object> *, int);
void display_tree(queue<BinaryNode*>, int);
int level;
float row_position;
object *data;
BinaryNode<object> *left;
BinaryNode<object> *right;
};
template<class object>
BinaryNode<object>::BinaryNode(object& node)
{
data = &node;
left = NULL;
right = NULL;
}
template<class object>
BinaryNode<object>::BinaryNode(BinaryNode<object> *node, int size)
{
breadth_first_search(node, size);
}
template<class object>
void BinaryNode<object>::breadth_first_search(BinaryNode<object> *node, int size)
{
int level_count=0;
float root_size = (float)size;
node->level=level_count;
root_size = ceil(root_size/2);
node->row_position = (int) root_size;
queue<BinaryNode*> queue_node;
queue_node.push(node);
Stack<BinaryNode<char>*> *ptrStack = new Stack<BinaryNode<char>*>;
ptrStack->push(node);
cout<<*node->data;
while(!queue_node.empty())
{
node = queue_node.front();
if(node->left != NULL)
{ int left_level_count =node->level;
node->left->level = ++left_level_count;
node->left->row_position= node->row_position+1;
queue_node.push(node->left);
ptrStack->push(node->left);
cout<<*node->left->data;
}
if(node->right != NULL)
{
int right_level_count = node->level;
node->right->level = ++right_level_count;
node->right->row_position= node->row_position-1;
queue_node.push(node->right);
ptrStack->push(node->right);
cout<<*node->right->data;
}
queue_node.pop();
}
display_tree(ptrStack, size);
}
template<class object>
void BinaryNode<object>::display_tree(Stack<BinaryNode<char>*> *stack, int size)
{
char array_display_tree = new char[size][size];
while(!stack->isEmpty())
{
char *currentNode(stack->topAndPop());
array_display_tree[currentNode->row_position][currentNode->level] = currentNode->data;
}
}
#endif
StackLi.h
#ifndef STACKLI_H_
#define STACKLI_H_
#include <stdlib.h>
#include<exception>
using namespace std;
#include"BinaryNode.h"
//#include "Except.h"
// Stack class -- linked list implementation.
//
// CONSTRUCTION: with no parameters.
//
// ******************PUBLIC OPERATIONS*********************
// void push( x ) --> Insert x
// void pop( ) --> Remove most recently inserted item
// Object top( ) --> Return most recently inserted item
// Object topAndPop( ) --> Return and remove most recently inserted item
// bool isEmpty( ) --> Return true if empty; else false
// void makeEmpty( ) --> Remove all items
// ******************ERRORS********************************
// UnderflowException thrown as needed.
template <class Object>
class Stack
{
public:
Stack( );
Stack( const Stack & );
~Stack( );
bool isEmpty( ) const;
const Object & top( ) const;
void makeEmpty( );
void pop( );
void push( const Object & );
Object topAndPop( );
const Stack & operator=( const Stack & );
private:
struct ListNode
{
Object element;
ListNode *next;
ListNode( const Object & theElement, ListNode * n = NULL )
: element( theElement ), next( n ) { }
};
ListNode *topOfStack;
};
//#include "StackLi.cpp"
// Construct the stack.
template <class Object>
Stack<Object>::Stack( )
{
topOfStack = NULL;
}
// Copy constructor.
template <class Object>
Stack<Object>::Stack( const Stack<Object> & rhs )
{
topOfStack = NULL;
*this = rhs;
}
template<>
Stack<BinaryNode<char>*>::Stack()
{
topOfStack = NULL;
}
// Destructor.
template <class Object>
Stack<Object>::~Stack( )
{
makeEmpty( );
}
// Test if the stack is logically empty.
// Return true if empty, false, otherwise.
template <class Object>
bool Stack<Object>::isEmpty( ) const
{
return topOfStack == NULL;
}
// Make the stack logically empty.
template <class Object>
void Stack<Object>::makeEmpty( )
{
while( !isEmpty( ) )
pop( );
}
// Return the most recently inserted item in the stack.
// or throw an UnderflowException if empty.
template <class Object>
const Object & Stack<Object>::top( ) const
{
//if( isEmpty( ) )
//throw UnderflowException( );
return topOfStack->element;
}
// Remove the most recently inserted item from the stack.
// Throw Underflow if the stack is empty.
template <class Object>
void Stack<Object>::pop( )
{
// if( isEmpty( ) )
// throw UnderflowException( );
ListNode *oldTop = topOfStack;
topOfStack = topOfStack->next;
delete oldTop;
}
// Return and remove the most recently inserted item
// from the stack.
template <class Object>
Object Stack<Object>::topAndPop( )
{
Object topItem = top( );
pop( );
return topItem;
}
// Insert x into the stack.
template <class Object>
void Stack<Object>::push( const Object & x )
{
topOfStack = new ListNode( x, topOfStack );
}
// Deep copy.
template <class Object>
const Stack<Object> & Stack<Object>::operator=( const Stack<Object> & rhs )
{
if( this != &rhs )
{
makeEmpty( );
if( rhs.isEmpty( ) )
return *this;
ListNode *rptr = rhs.topOfStack;
ListNode *ptr = new ListNode( rptr->element );
topOfStack = ptr;
for( rptr = rptr->next; rptr != NULL; rptr = rptr->next )
ptr = ptr->next = new ListNode( rptr->element );
}
return *this;
}
#endif
Main.cpp
#include<iostream>
#include<string>
#include<cstring>
#include<sstream>
#include<iterator>
#include<vector>
#include<algorithm>
using namespace std;
#include"StackLi.h"
#include"BinaryNode.h"
bool lower_procedence(char *tokenPtr, char top_of_stack)
{
// Checks if operaotr just read in is of higher or equal presedence
// than the operator next on the stack
bool flag = true;
if ((*tokenPtr == '+') && (top_of_stack == '+'))
{
flag = true;
}
else if ((*tokenPtr == '-') && (top_of_stack == '-'))
{
flag = true;
}
else if ((*tokenPtr == '-') && (top_of_stack == '+') )
{
flag = true;
}
else if ((*tokenPtr == '+') && (top_of_stack == '-'))
{
flag = false;
}
else if(((*tokenPtr == '+') || (*tokenPtr == '-')) && ((top_of_stack == '*') || (top_of_stack == '/')))
{
flag= true;
}
else if ((*tokenPtr == '*') && (top_of_stack == '/'))
{
flag = false;
}
else if ((*tokenPtr == '*') && (top_of_stack == '*'))
{
flag = true;
}
else if ((*tokenPtr == '/') && (top_of_stack == '/'))
{
flag = true;
}
else if ((*tokenPtr == '/') && (top_of_stack == '*'))
{
flag = true;
}
else if(((*tokenPtr == '*') || (*tokenPtr == '/')) && ((top_of_stack == '+') || (top_of_stack == '-')))
{
flag= false;
}
else if((*tokenPtr == ')')&& (top_of_stack != '(') )
{
flag= false;
}
else if ((*tokenPtr == ')') && ( top_of_stack == '('))
{
flag = true;
}
else if(top_of_stack == '(')
flag = false;
else
flag = true;
return flag;
}
int main()
{
Stack<char> * operator_Stack = new Stack<char>;
Stack<BinaryNode<char>*> * ptrStack = new Stack<BinaryNode<char>*>;
char equation[50];
char* tokenPtr;
//vector<char> postfix_Equations(1, 0);
vector<char> postfix_Equations;
cout<<"Please Enter In A Equation"<<endl;
cin.getline(equation, sizeof(equation), '\n');//To catch all of the char p
tokenPtr = strtok(equation," ");
while(tokenPtr != NULL)
{
if( *tokenPtr == '+' || *tokenPtr=='-' || *tokenPtr == '/' || *tokenPtr == '*' || *tokenPtr == '^')
{
while(!operator_Stack->isEmpty())
{
if(lower_procedence(tokenPtr, operator_Stack->top()))
{
postfix_Equations.push_back(operator_Stack->topAndPop());
}
else
{
operator_Stack->push(*tokenPtr);
break;
}
}
if(operator_Stack->isEmpty())
operator_Stack->push(*tokenPtr);
}
else if (*tokenPtr == '(')
operator_Stack->push(*tokenPtr);
else if(*tokenPtr == ')')
{
while((operator_Stack->top() != '(')&& (!operator_Stack->isEmpty()))
postfix_Equations.push_back(operator_Stack->topAndPop());
if( operator_Stack->top() == '(' || operator_Stack->top() == ')' )
{
operator_Stack->pop();
}
}
else if(isalpha(*tokenPtr) || isalnum(*tokenPtr))
postfix_Equations.push_back(*tokenPtr);
tokenPtr = strtok(NULL, " ");
}
if(!operator_Stack->isEmpty())
{
while(!operator_Stack->isEmpty())
postfix_Equations.push_back(operator_Stack->topAndPop());
}
cout<<"\nPostfix Equation\n"<<endl;
for(int i =0; i!= postfix_Equations.size(); i++)
cout<<postfix_Equations[i];
cout<<"\n"<<endl;
//End of POSTFIX---------------------------------------------------------------------------------------------------
for(int i = 0; i < postfix_Equations.size(); i++)
{
if(postfix_Equations[i] == '(' || postfix_Equations[i] == ')' || postfix_Equations[i] == '+' || postfix_Equations[i]=='-' || postfix_Equations[i] == '/' || postfix_Equations[i] == '*' || postfix_Equations[i] == '^')
{
if(ptrStack->isEmpty())
{
BinaryNode<char> *newNode = new BinaryNode<char>(postfix_Equations[i]);
ptrStack->push(newNode);
}
else
{
BinaryNode<char> *newNode = new BinaryNode<char>(postfix_Equations[i]);
newNode->right = ptrStack->topAndPop();
if(!ptrStack->isEmpty())
{
newNode->left = ptrStack->topAndPop();
}
ptrStack->push(newNode);
}
}
else if(postfix_Equations[i] == '~' || postfix_Equations[i] == '!' || postfix_Equations[i] == '&')
{
BinaryNode<char> *newNode = new BinaryNode<char>(postfix_Equations[i]);
newNode->right = ptrStack->topAndPop();
ptrStack->push(newNode);
}
else
{
BinaryNode<char> *newNode = new BinaryNode<char>(postfix_Equations[i]);
ptrStack->push(newNode);
}
tokenPtr = strtok(NULL, " ");
}
BinaryNode<char> *finalNode = new BinaryNode<char>(ptrStack->topAndPop(), postfix_Equations.size());
system("PAUSE");
return 0;
}
Ooops, the code snippets seem to be messed up - that one was missing:
template<class object>
void BinaryNode<object>::display_tree(Stack<BinaryNode<char>*> *stack, int size)
{
array_display_tree.reserve(size);
for (int i = 0; i < size; ++i)
array_display_tree[i].reserve(size);
while(!stack->isEmpty())
{
char *currentNode(stack->topAndPop());
array_display_tree[currentNode->row_position][currentNode->level] = currentNode->data;
}
}
ASKER
I am still receiving all of the same errors. There are a few errors that are sticking out to me and I do not know how to fix it.
Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ stackli.h 62 1 BinaryTree
Error 3 error C2988: unrecognizable template declaration/definition c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ binarynode .h 117 1 BinaryTree
Error 7 error C2988: unrecognizable template declaration/definition c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ stackli.h 62 1 BinaryTree
I think this is were the problem is coming from. But, my template's declaration is the same as the definition.
Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\owner\documents\v
Error 3 error C2988: unrecognizable template declaration/definition c:\users\owner\documents\v
Error 7 error C2988: unrecognizable template declaration/definition c:\users\owner\documents\v
I think this is were the problem is coming from. But, my template's declaration is the same as the definition.
ASKER
Any suggestions would be greatly appreciated.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Disregard my last comment, there was more at the door - here's the correct declaration and the implementation:
template<class object>
class BinaryNode
{
public:
BinaryNode(object&);
BinaryNode(BinaryNode<object> * , int );
void breadth_first_search(BinaryNode<object> *, int);
void display_tree(Stack<BinaryNode<object>* > *stack, int size);
int level;
float row_position;
object *data;
BinaryNode<object> *left;
BinaryNode<object> *right;
};
// ...
template<class object>
void BinaryNode<object>::display_tree(Stack<BinaryNode<object>*> *stack, int size)
{
vector<vector<object*> > array_display_tree;
array_display_tree.reserve(size);
for (int i = 0; i < size; ++i) array_display_tree[i].reserve(size);
while(!stack->isEmpty())
{
BinaryNode<object> *currentNode(stack->topAndPop());
array_display_tree[currentNode->row_position][currentNode->level] = currentNode->data;
}
}
ASKER
Hi jkr,
I am getting the same errors I do not know if this matters but I am using Visual Studio 2010. I just did not know if the program is working for you if so what compiler are you using?
Thanks For The Help
I am getting the same errors I do not know if this matters but I am using Visual Studio 2010. I just did not know if the program is working for you if so what compiler are you using?
Thanks For The Help
This is VS2005 and it compiles fine for me - the problem was a) that you were using 'char' where a template parameter was required and b) that the declaration did not match. Have you replaced both the 'BinaryNode' class as well as the method's body?
ASKER
Yes I have replaced both of them
What errors are you getting now? (I got a buckload of warnings, but nothing serious)
ASKER
Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ stackli.h 62 1 BinaryTree
Error 12 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ stackli.h 79 1 BinaryTree
Error 3 error C2988: unrecognizable template declaration/definition c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ binarynode .h 126 1 BinaryTree
Error 7 error C2988: unrecognizable template declaration/definition c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ stackli.h 62 1 BinaryTree
Error 14 error C2988: unrecognizable template declaration/definition c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ stackli.h 79 1 BinaryTree
Error 16 error C2588: '::~Stack' : illegal global destructor c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ stackli.h 87 1 BinaryTree
Error 10 error C2447: '{' : missing function header (old-style formal list?) c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ stackli.h 72 1 BinaryTree
Error 5 error C2143: syntax error : missing ';' before '<' c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ stackli.h 62 1 BinaryTree
Error 11 error C2143: syntax error : missing ';' before '<' c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ stackli.h 79 1 BinaryTree
Error 9 error C2143: syntax error : missing ';' before '{' c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ stackli.h 72 1 BinaryTree
Error 13 error C2086: 'int Stack' : redefinition c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ stackli.h 79 1 BinaryTree
Error 2 error C2065: 'Stack' : undeclared identifier c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ binarynode .h 126 1 BinaryTree
Error 1 error C2061: syntax error : identifier 'Stack' c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ binarynode .h 25 1 BinaryTree
Error 4 error C2059: syntax error : '>' c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ binarynode .h 126 1 BinaryTree
Error 8 error C2059: syntax error : '<' c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ stackli.h 62 1 BinaryTree
Error 15 error C2059: syntax error : '<' c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ stackli.h 79 1 BinaryTree
Error 17 error C1903: unable to recover from previous error(s); stopping compilation c:\users\owner\documents\v isual studio 2010\projects\binarytree\b inarytree\ stackli.h 87 1 BinaryTree
Error 12 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\owner\documents\v
Error 3 error C2988: unrecognizable template declaration/definition c:\users\owner\documents\v
Error 7 error C2988: unrecognizable template declaration/definition c:\users\owner\documents\v
Error 14 error C2988: unrecognizable template declaration/definition c:\users\owner\documents\v
Error 16 error C2588: '::~Stack' : illegal global destructor c:\users\owner\documents\v
Error 10 error C2447: '{' : missing function header (old-style formal list?) c:\users\owner\documents\v
Error 5 error C2143: syntax error : missing ';' before '<' c:\users\owner\documents\v
Error 11 error C2143: syntax error : missing ';' before '<' c:\users\owner\documents\v
Error 9 error C2143: syntax error : missing ';' before '{' c:\users\owner\documents\v
Error 13 error C2086: 'int Stack' : redefinition c:\users\owner\documents\v
Error 2 error C2065: 'Stack' : undeclared identifier c:\users\owner\documents\v
Error 1 error C2061: syntax error : identifier 'Stack' c:\users\owner\documents\v
Error 4 error C2059: syntax error : '>' c:\users\owner\documents\v
Error 8 error C2059: syntax error : '<' c:\users\owner\documents\v
Error 15 error C2059: syntax error : '<' c:\users\owner\documents\v
Error 17 error C1903: unable to recover from previous error(s); stopping compilation c:\users\owner\documents\v
Could you post your actual code? Otherwise it's hard to match the lines...
ASKER
BinaryNode.h
StackLi.h
Main.cpp
#ifndef BINARYNODE_H
#define BINARYNODE_H
#include<iomanip>
#include<iostream>
#include<queue>
#include<string>
#include<math.h>
#include<vector>
using namespace std;
#include"StackLi.h"
template<class object>
class BinaryNode
{
public:
BinaryNode(object&);
BinaryNode(BinaryNode<object> * , int );
void breadth_first_search(BinaryNode<object> *, int);
void display_tree(Stack<BinaryNode<object>* > *stack, int size);
int level;
float row_position;
object *data;
BinaryNode<object> *left;
BinaryNode<object> *right;
};
template<class object>
BinaryNode<object>::BinaryNode(object& node)
{
data = &node;
left = NULL;
right = NULL;
}
template<class object>
BinaryNode<object>::BinaryNode(BinaryNode<object> *node, int size)
{
breadth_first_search(node, size);
}
template<class object>
void BinaryNode<object>::breadth_first_search(BinaryNode<object> *node, int size)
{
tree_format.reserve(size);
for(int i =0; i< tree_format.size(); i++)
tree_format[i].reserve(size);
int level_count=0;
float root_size = (float)size;
node->level=level_count;
root_size = ceil(root_size/2);
node->row_position = (int) root_size;
queue<BinaryNode*> queue_node;
queue_node.push(node);
Stack<BinaryNode<char>*> *ptrStack = new Stack<BinaryNode<char>*>;
ptrStack->push(node);
cout<<*node->data;
while(!queue_node.empty())
{
node = queue_node.front();
if(node->left != NULL)
{ int left_level_count =node->level;
node->left->level = ++left_level_count;
node->left->row_position= node->row_position+1;
queue_node.push(node->left);
ptrStack->push(node->left);
cout<<*node->left->data;
}
if(node->right != NULL)
{
int right_level_count = node->level;
node->right->level = ++right_level_count;
node->right->row_position= node->row_position-1;
queue_node.push(node->right);
ptrStack->push(node->right);
cout<<*node->right->data;
}
BinaryNode *currentNode(queue_node.front());
tree_format[currentNode->row_position][currentNode->level]= currentNode->data;
}
display_tree(ptrStack, size);
}
template<class object>
void BinaryNode<object>::display_tree(Stack<BinaryNode<object>*> *stack, int size)
{
vector<vector<object*> > array_display_tree;
array_display_tree.reserve(size);
for (int i = 0; i < size; ++i)
array_display_tree[i].reserve(size);
while(!stack->isEmpty())
{
BinaryNode<object> *currentNode(stack->topAndPop());
array_display_tree[currentNode->row_position][currentNode->level] = currentNode->data;
}
}
#endif
StackLi.h
#ifndef STACKLI_H_
#define STACKLI_H_
#include <stdlib.h>
#include<exception>
using namespace std;
#include"BinaryNode.h"
//#include "Except.h"
// Stack class -- linked list implementation.
//
// CONSTRUCTION: with no parameters.
//
// ******************PUBLIC OPERATIONS*********************
// void push( x ) --> Insert x
// void pop( ) --> Remove most recently inserted item
// Object top( ) --> Return most recently inserted item
// Object topAndPop( ) --> Return and remove most recently inserted item
// bool isEmpty( ) --> Return true if empty; else false
// void makeEmpty( ) --> Remove all items
// ******************ERRORS********************************
// UnderflowException thrown as needed.
template <class Object>
class Stack
{
public:
Stack( );
Stack( const Stack & );
~Stack( );
bool isEmpty( ) const;
const Object & top( ) const;
void makeEmpty( );
void pop( );
void push( const Object & );
Object topAndPop( );
const Stack & operator=( const Stack & );
private:
struct ListNode
{
Object element;
ListNode *next;
ListNode( const Object & theElement, ListNode * n = NULL )
: element( theElement ), next( n ) { }
};
ListNode *topOfStack;
};
//#include "StackLi.cpp"
// Construct the stack.
template <class Object>
Stack<Object>::Stack( )
{
topOfStack = NULL;
}
// Copy constructor.
template <class Object>
Stack<Object>::Stack( const Stack<Object> & rhs )
{
topOfStack = NULL;
*this = rhs;
}
template<>
Stack<BinaryNode<char>*>::Stack()
{
topOfStack = NULL;
}
// Destructor.
template <class Object>
Stack<Object>::~Stack()
{
makeEmpty( );
}
// Test if the stack is logically empty.
// Return true if empty, false, otherwise.
template <class Object>
bool Stack<Object>::isEmpty( ) const
{
return topOfStack == NULL;
}
// Make the stack logically empty.
template <class Object>
void Stack<Object>::makeEmpty( )
{
while( !isEmpty( ) )
pop( );
}
// Return the most recently inserted item in the stack.
// or throw an UnderflowException if empty.
template <class Object>
const Object & Stack<Object>::top( ) const
{
//if( isEmpty( ) )
//throw UnderflowException( );
return topOfStack->element;
}
// Remove the most recently inserted item from the stack.
// Throw Underflow if the stack is empty.
template <class Object>
void Stack<Object>::pop( )
{
// if( isEmpty( ) )
// throw UnderflowException( );
ListNode *oldTop = topOfStack;
topOfStack = topOfStack->next;
delete oldTop;
}
// Return and remove the most recently inserted item
// from the stack.
template <class Object>
Object Stack<Object>::topAndPop( )
{
Object topItem = top( );
pop( );
return topItem;
}
// Insert x into the stack.
template <class Object>
void Stack<Object>::push( const Object & x )
{
topOfStack = new ListNode( x, topOfStack );
}
// Deep copy.
template <class Object>
const Stack<Object> & Stack<Object>::operator=( const Stack<Object> & rhs )
{
if( this != &rhs )
{
makeEmpty( );
if( rhs.isEmpty( ) )
return *this;
ListNode *rptr = rhs.topOfStack;
ListNode *ptr = new ListNode( rptr->element );
topOfStack = ptr;
for( rptr = rptr->next; rptr != NULL; rptr = rptr->next )
ptr = ptr->next = new ListNode( rptr->element );
}
return *this;
}
#endif
Main.cpp
#include<iostream>
#include<string>
#include<cstring>
#include<sstream>
#include<iterator>
#include<vector>
#include<algorithm>
using namespace std;
#include"StackLi.h"
#include"BinaryNode.h"
bool lower_procedence(char *tokenPtr, char top_of_stack)
{
// Checks if operaotr just read in is of higher or equal presedence
// than the operator next on the stack
bool flag = true;
if ((*tokenPtr == '+') && (top_of_stack == '+'))
{
flag = true;
}
else if ((*tokenPtr == '-') && (top_of_stack == '-'))
{
flag = true;
}
else if ((*tokenPtr == '-') && (top_of_stack == '+') )
{
flag = true;
}
else if ((*tokenPtr == '+') && (top_of_stack == '-'))
{
flag = false;
}
else if(((*tokenPtr == '+') || (*tokenPtr == '-')) && ((top_of_stack == '*') || (top_of_stack == '/')))
{
flag= true;
}
else if ((*tokenPtr == '*') && (top_of_stack == '/'))
{
flag = false;
}
else if ((*tokenPtr == '*') && (top_of_stack == '*'))
{
flag = true;
}
else if ((*tokenPtr == '/') && (top_of_stack == '/'))
{
flag = true;
}
else if ((*tokenPtr == '/') && (top_of_stack == '*'))
{
flag = true;
}
else if(((*tokenPtr == '*') || (*tokenPtr == '/')) && ((top_of_stack == '+') || (top_of_stack == '-')))
{
flag= false;
}
else if((*tokenPtr == ')')&& (top_of_stack != '(') )
{
flag= false;
}
else if ((*tokenPtr == ')') && ( top_of_stack == '('))
{
flag = true;
}
else if(top_of_stack == '(')
flag = false;
else
flag = true;
return flag;
}
int main()
{
Stack<char> * operator_Stack = new Stack<char>;
Stack<BinaryNode<char>*> * ptrStack = new Stack<BinaryNode<char>*>;
char equation[50];
char* tokenPtr;
//vector<char> postfix_Equations(1, 0);
vector<char> postfix_Equations;
cout<<"Please Enter In A Equation"<<endl;
cin.getline(equation, sizeof(equation), '\n');//To catch all of the char p
tokenPtr = strtok(equation," ");
while(tokenPtr != NULL)
{
if( *tokenPtr == '+' || *tokenPtr=='-' || *tokenPtr == '/' || *tokenPtr == '*' || *tokenPtr == '^')
{
while(!operator_Stack->isEmpty())
{
if(lower_procedence(tokenPtr, operator_Stack->top()))
{
postfix_Equations.push_back(operator_Stack->topAndPop());
}
else
{
operator_Stack->push(*tokenPtr);
break;
}
}
if(operator_Stack->isEmpty())
operator_Stack->push(*tokenPtr);
}
else if (*tokenPtr == '(')
operator_Stack->push(*tokenPtr);
else if(*tokenPtr == ')')
{
while((operator_Stack->top() != '(')&& (!operator_Stack->isEmpty()))
postfix_Equations.push_back(operator_Stack->topAndPop());
if( operator_Stack->top() == '(' || operator_Stack->top() == ')' )
{
operator_Stack->pop();
}
}
else if(isalpha(*tokenPtr) || isalnum(*tokenPtr))
postfix_Equations.push_back(*tokenPtr);
tokenPtr = strtok(NULL, " ");
}
if(!operator_Stack->isEmpty())
{
while(!operator_Stack->isEmpty())
postfix_Equations.push_back(operator_Stack->topAndPop());
}
cout<<"\nPostfix Equation\n"<<endl;
for(int i =0; i!= postfix_Equations.size(); i++)
cout<<postfix_Equations[i];
cout<<"\n"<<endl;
//End of POSTFIX---------------------------------------------------------------------------------------------------
for(int i = 0; i < postfix_Equations.size(); i++)
{
if(postfix_Equations[i] == '(' || postfix_Equations[i] == ')' || postfix_Equations[i] == '+' || postfix_Equations[i]=='-' || postfix_Equations[i] == '/' || postfix_Equations[i] == '*' || postfix_Equations[i] == '^')
{
if(ptrStack->isEmpty())
{
BinaryNode<char> *newNode = new BinaryNode<char>(postfix_Equations[i]);
ptrStack->push(newNode);
}
else
{
BinaryNode<char> *newNode = new BinaryNode<char>(postfix_Equations[i]);
newNode->right = ptrStack->topAndPop();
if(!ptrStack->isEmpty())
{
newNode->left = ptrStack->topAndPop();
}
ptrStack->push(newNode);
}
}
else if(postfix_Equations[i] == '~' || postfix_Equations[i] == '!' || postfix_Equations[i] == '&')
{
BinaryNode<char> *newNode = new BinaryNode<char>(postfix_Equations[i]);
newNode->right = ptrStack->topAndPop();
ptrStack->push(newNode);
}
else
{
BinaryNode<char> *newNode = new BinaryNode<char>(postfix_Equations[i]);
ptrStack->push(newNode);
}
tokenPtr = strtok(NULL, " ");
}
BinaryNode<char> *finalNode = new BinaryNode<char>(ptrStack->topAndPop(), postfix_Equations.size());
system("PAUSE");
return 0;
}
Um, that is not the same code as in your original Q, thus causing other issues (there was no template specialization in there before) - could you try with that original one plus the modifications?
ASKER
So, I went back to the old code and did the modification on that code and I am still getting the same errors.
BinaryNode.h
StackLi.h
Main.cpp
BinaryNode.h
#ifndef BINARYNODE_H
#define BINARYNODE_H
#include<iomanip>
#include<iostream>
#include<queue>
#include<string>
#include<math.h>
using namespace std;
#include"StackLi.h"
template<class object>
class BinaryNode
{
public:
BinaryNode(object&);
BinaryNode(BinaryNode<object> * , int );
void breadth_first_search(BinaryNode<object> *, int);
void display_tree(Stack<BinaryNode<object>* > *stack, int size);
int level;
float row_position;
object *data;
BinaryNode<object> *left;
BinaryNode<object> *right;
};
template<class object>
BinaryNode<object>::BinaryNode(object& node)
{
data = &node;
left = NULL;
right = NULL;
}
template<class object>
BinaryNode<object>::BinaryNode(BinaryNode<object> *node, int size)
{
breadth_first_search(node, size);
}
template<class object>
void BinaryNode<object>::breadth_first_search(BinaryNode<object> *node, int size)
{
int level_count=0;
float root_size = (float)size;
node->level=level_count;
root_size = ceil(root_size/2);
node->row_position = (int) root_size;
queue<BinaryNode*> queue_node;
queue_node.push(node);
Stack<BinaryNode<char>*> *ptrStack = new Stack<BinaryNode<char>*>;
ptrStack->push(node);
cout<<*node->data;
while(!queue_node.empty())
{
node = queue_node.front();
if(node->left != NULL)
{ int left_level_count =node->level;
node->left->level = ++left_level_count;
node->left->row_position= node->row_position+1;
queue_node.push(node->left);
ptrStack->push(node->left);
cout<<*node->left->data;
}
if(node->right != NULL)
{
int right_level_count = node->level;
node->right->level = ++right_level_count;
node->right->row_position= node->row_position-1;
queue_node.push(node->right);
ptrStack->push(node->right);
cout<<*node->right->data;
}
queue_node.pop();
}
display_tree(ptrStack, size);
}
template<class object>
void BinaryNode<object>::display_tree(Stack<BinaryNode<object>*> *stack, int size)
{
vector<vector<object*> > array_display_tree;
array_display_tree.reserve(size);
for (int i = 0; i < size; ++i) array_display_tree[i].reserve(size);
while(!stack->isEmpty())
{
BinaryNode<object> *currentNode(stack->topAndPop());
array_display_tree[currentNode->row_position][currentNode->level] = currentNode->data;
}
}
#endif
StackLi.h
#ifndef STACKLI_H_
#define STACKLI_H_
#include <stdlib.h>
#include<exception>
using namespace std;
#include"BinaryNode.h"
//#include "Except.h"
// Stack class -- linked list implementation.
//
// CONSTRUCTION: with no parameters.
//
// ******************PUBLIC OPERATIONS*********************
// void push( x ) --> Insert x
// void pop( ) --> Remove most recently inserted item
// Object top( ) --> Return most recently inserted item
// Object topAndPop( ) --> Return and remove most recently inserted item
// bool isEmpty( ) --> Return true if empty; else false
// void makeEmpty( ) --> Remove all items
// ******************ERRORS********************************
// UnderflowException thrown as needed.
template <class Object>
class Stack
{
public:
Stack( );
Stack( const Stack & );
~Stack( );
bool isEmpty( ) const;
const Object & top( ) const;
void makeEmpty( );
void pop( );
void push( const Object & );
Object topAndPop( );
const Stack & operator=( const Stack & );
private:
struct ListNode
{
Object element;
ListNode *next;
ListNode( const Object & theElement, ListNode * n = NULL )
: element( theElement ), next( n ) { }
};
ListNode *topOfStack;
};
//#include "StackLi.cpp"
// Construct the stack.
template <class Object>
Stack<Object>::Stack( )
{
topOfStack = NULL;
}
// Copy constructor.
template <class Object>
Stack<Object>::Stack( const Stack<Object> & rhs )
{
topOfStack = NULL;
*this = rhs;
}
template<>
Stack<BinaryNode<char>*>::Stack()
{
topOfStack = NULL;
}
// Destructor.
template <class Object>
Stack<Object>::~Stack( )
{
makeEmpty( );
}
// Test if the stack is logically empty.
// Return true if empty, false, otherwise.
template <class Object>
bool Stack<Object>::isEmpty( ) const
{
return topOfStack == NULL;
}
// Make the stack logically empty.
template <class Object>
void Stack<Object>::makeEmpty( )
{
while( !isEmpty( ) )
pop( );
}
// Return the most recently inserted item in the stack.
// or throw an UnderflowException if empty.
template <class Object>
const Object & Stack<Object>::top( ) const
{
//if( isEmpty( ) )
//throw UnderflowException( );
return topOfStack->element;
}
// Remove the most recently inserted item from the stack.
// Throw Underflow if the stack is empty.
template <class Object>
void Stack<Object>::pop( )
{
// if( isEmpty( ) )
// throw UnderflowException( );
ListNode *oldTop = topOfStack;
topOfStack = topOfStack->next;
delete oldTop;
}
// Return and remove the most recently inserted item
// from the stack.
template <class Object>
Object Stack<Object>::topAndPop( )
{
Object topItem = top( );
pop( );
return topItem;
}
// Insert x into the stack.
template <class Object>
void Stack<Object>::push( const Object & x )
{
topOfStack = new ListNode( x, topOfStack );
}
// Deep copy.
template <class Object>
const Stack<Object> & Stack<Object>::operator=( const Stack<Object> & rhs )
{
if( this != &rhs )
{
makeEmpty( );
if( rhs.isEmpty( ) )
return *this;
ListNode *rptr = rhs.topOfStack;
ListNode *ptr = new ListNode( rptr->element );
topOfStack = ptr;
for( rptr = rptr->next; rptr != NULL; rptr = rptr->next )
ptr = ptr->next = new ListNode( rptr->element );
}
return *this;
}
#endif
Main.cpp
#include<iostream>
#include<string>
#include<cstring>
#include<sstream>
#include<iterator>
#include<vector>
#include<algorithm>
using namespace std;
#include"StackLi.h"
#include"BinaryNode.h"
bool lower_procedence(char *tokenPtr, char top_of_stack)
{
// Checks if operaotr just read in is of higher or equal presedence
// than the operator next on the stack
bool flag = true;
if ((*tokenPtr == '+') && (top_of_stack == '+'))
{
flag = true;
}
else if ((*tokenPtr == '-') && (top_of_stack == '-'))
{
flag = true;
}
else if ((*tokenPtr == '-') && (top_of_stack == '+') )
{
flag = true;
}
else if ((*tokenPtr == '+') && (top_of_stack == '-'))
{
flag = false;
}
else if(((*tokenPtr == '+') || (*tokenPtr == '-')) && ((top_of_stack == '*') || (top_of_stack == '/')))
{
flag= true;
}
else if ((*tokenPtr == '*') && (top_of_stack == '/'))
{
flag = false;
}
else if ((*tokenPtr == '*') && (top_of_stack == '*'))
{
flag = true;
}
else if ((*tokenPtr == '/') && (top_of_stack == '/'))
{
flag = true;
}
else if ((*tokenPtr == '/') && (top_of_stack == '*'))
{
flag = true;
}
else if(((*tokenPtr == '*') || (*tokenPtr == '/')) && ((top_of_stack == '+') || (top_of_stack == '-')))
{
flag= false;
}
else if((*tokenPtr == ')')&& (top_of_stack != '(') )
{
flag= false;
}
else if ((*tokenPtr == ')') && ( top_of_stack == '('))
{
flag = true;
}
else if(top_of_stack == '(')
flag = false;
else
flag = true;
return flag;
}
int main()
{
Stack<char> * operator_Stack = new Stack<char>;
Stack<BinaryNode<char>*> * ptrStack = new Stack<BinaryNode<char>*>;
char equation[50];
char* tokenPtr;
//vector<char> postfix_Equations(1, 0);
vector<char> postfix_Equations;
cout<<"Please Enter In A Equation"<<endl;
cin.getline(equation, sizeof(equation), '\n');//To catch all of the char p
tokenPtr = strtok(equation," ");
while(tokenPtr != NULL)
{
if( *tokenPtr == '+' || *tokenPtr=='-' || *tokenPtr == '/' || *tokenPtr == '*' || *tokenPtr == '^')
{
while(!operator_Stack->isEmpty())
{
if(lower_procedence(tokenPtr, operator_Stack->top()))
{
postfix_Equations.push_back(operator_Stack->topAndPop());
}
else
{
operator_Stack->push(*tokenPtr);
break;
}
}
if(operator_Stack->isEmpty())
operator_Stack->push(*tokenPtr);
}
else if (*tokenPtr == '(')
operator_Stack->push(*tokenPtr);
else if(*tokenPtr == ')')
{
while((operator_Stack->top() != '(')&& (!operator_Stack->isEmpty()))
postfix_Equations.push_back(operator_Stack->topAndPop());
if( operator_Stack->top() == '(' || operator_Stack->top() == ')' )
{
operator_Stack->pop();
}
}
else if(isalpha(*tokenPtr) || isalnum(*tokenPtr))
postfix_Equations.push_back(*tokenPtr);
tokenPtr = strtok(NULL, " ");
}
if(!operator_Stack->isEmpty())
{
while(!operator_Stack->isEmpty())
postfix_Equations.push_back(operator_Stack->topAndPop());
}
cout<<"\nPostfix Equation\n"<<endl;
for(int i =0; i!= postfix_Equations.size(); i++)
cout<<postfix_Equations[i];
cout<<"\n"<<endl;
//End of POSTFIX---------------------------------------------------------------------------------------------------
for(int i = 0; i < postfix_Equations.size(); i++)
{
if(postfix_Equations[i] == '(' || postfix_Equations[i] == ')' || postfix_Equations[i] == '+' || postfix_Equations[i]=='-' || postfix_Equations[i] == '/' || postfix_Equations[i] == '*' || postfix_Equations[i] == '^')
{
if(ptrStack->isEmpty())
{
BinaryNode<char> *newNode = new BinaryNode<char>(postfix_Equations[i]);
ptrStack->push(newNode);
}
else
{
BinaryNode<char> *newNode = new BinaryNode<char>(postfix_Equations[i]);
newNode->right = ptrStack->topAndPop();
if(!ptrStack->isEmpty())
{
newNode->left = ptrStack->topAndPop();
}
ptrStack->push(newNode);
}
}
else if(postfix_Equations[i] == '~' || postfix_Equations[i] == '!' || postfix_Equations[i] == '&')
{
BinaryNode<char> *newNode = new BinaryNode<char>(postfix_Equations[i]);
newNode->right = ptrStack->topAndPop();
ptrStack->push(newNode);
}
else
{
BinaryNode<char> *newNode = new BinaryNode<char>(postfix_Equations[i]);
ptrStack->push(newNode);
}
tokenPtr = strtok(NULL, " ");
}
BinaryNode<char> *finalNode = new BinaryNode<char>(ptrStack->topAndPop(), postfix_Equations.size());
system("PAUSE");
return 0;
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window
ince it neither is syntactically correct nor does it make sense - 'new' always returns a pointer, you are trying to assign it to 'char'. Then, 'new' cannot be used that way to create a 2D array. Try to replace that with
vector<vector<char> > array_display_tree;
or
vector<string> array_display_tree;
and
I.e.
Open in new window