Solved

Numerous Errors

Posted on 2011-03-13
16
401 Views
Last Modified: 2012-05-11
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.

 
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;

	}


}

Open in new window


The errors that I am receiving are:

1>c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\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\binarytree\stackli.h(62): error C2988: unrecognizable template declaration/definition
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h(62): error C2059: syntax error : '<'
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h(70): error C2143: syntax error : missing ';' before '{'
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h(70): error C2447: '{' : missing function header (old-style formal list?)
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h(77): error C2143: syntax error : missing ';' before '<'
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\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\binarytree\stackli.h(77): error C2086: 'int Stack' : redefinition
1>          c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h(62) : see declaration of 'Stack'
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h(77): error C2988: unrecognizable template declaration/definition
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h(77): error C2059: syntax error : '<'
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h(85): error C2588: '::~Stack' : illegal global destructor
1>c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\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

Open in new window


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

Open in new window


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;
}

Open in new window

0
Comment
Question by:brich744
  • 8
  • 7
16 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
The problem is most likely the line
char array_display_tree = new char[size][size];

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.
array_display_tree.reserve(size);

for (int i = 0; i < size; ++i) varray_display_tree[i].reserve(size);

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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;

	}


}

Open in new window

0
 

Author Comment

by:brich744
Comment Utility
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\visual studio 2010\projects\binarytree\binarytree\stackli.h      62      1      BinaryTree

Error      3      error C2988: unrecognizable template declaration/definition      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\binarynode.h      117      1      BinaryTree
Error      7      error C2988: unrecognizable template declaration/definition      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\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.
0
 

Author Comment

by:brich744
Comment Utility
Any suggestions would be greatly appreciated.
0
 
LVL 86

Accepted Solution

by:
jkr earned 300 total points
Comment Utility
Ah, I think I spotted something - change

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;

      }


}

into the following (the template hould be applied to the 1st argument as well)

template<class object>
void BinaryNode<object>::display_tree(Stack<BinaryNode<object>> *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;

      }


}

Also, you are declaring that method as

      void display_tree(queue<BinaryNode*>, int);


Shouldn't that be

        void display_tree(Stack<BinaryNode<object>> *stack, int size);

?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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;

	}


}

Open in new window

0
 

Author Comment

by:brich744
Comment Utility
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
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:brich744
Comment Utility
Yes I have replaced both of them
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
What errors are you getting now? (I got a buckload of warnings, but nothing serious)
0
 

Author Comment

by:brich744
Comment Utility
Error      6      error C4430: missing type specifier - int assumed. Note: C++ does not support default-int      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\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\visual studio 2010\projects\binarytree\binarytree\stackli.h      79      1      BinaryTree
Error      3      error C2988: unrecognizable template declaration/definition      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\binarynode.h      126      1      BinaryTree
Error      7      error C2988: unrecognizable template declaration/definition      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h      62      1      BinaryTree
Error      14      error C2988: unrecognizable template declaration/definition      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h      79      1      BinaryTree
Error      16      error C2588: '::~Stack' : illegal global destructor      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h      87      1      BinaryTree
Error      10      error C2447: '{' : missing function header (old-style formal list?)      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h      72      1      BinaryTree
Error      5      error C2143: syntax error : missing ';' before '<'      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h      62      1      BinaryTree
Error      11      error C2143: syntax error : missing ';' before '<'      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h      79      1      BinaryTree
Error      9      error C2143: syntax error : missing ';' before '{'      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h      72      1      BinaryTree
Error      13      error C2086: 'int Stack' : redefinition      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h      79      1      BinaryTree
Error      2      error C2065: 'Stack' : undeclared identifier      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\binarynode.h      126      1      BinaryTree
Error      1      error C2061: syntax error : identifier 'Stack'      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\binarynode.h      25      1      BinaryTree
Error      4      error C2059: syntax error : '>'      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\binarynode.h      126      1      BinaryTree
Error      8      error C2059: syntax error : '<'      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h      62      1      BinaryTree
Error      15      error C2059: syntax error : '<'      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h      79      1      BinaryTree
Error      17      error C1903: unable to recover from previous error(s); stopping compilation      c:\users\owner\documents\visual studio 2010\projects\binarytree\binarytree\stackli.h      87      1      BinaryTree
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Could you post your actual code? Otherwise it's hard to match the lines...
0
 

Author Comment

by:brich744
Comment Utility
BinaryNode.h
 
#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

Open in new window


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

Open in new window


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;
}

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
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?
0
 

Author Comment

by:brich744
Comment Utility
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
 
#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

Open in new window


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

Open in new window


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;
}

Open in new window

0
 
LVL 32

Assisted Solution

by:sarabande
sarabande earned 200 total points
Comment Utility
you have a cyclic inlude. the binarynode.h includes stackli.h and stackli.h includes binarynode.h.

because of that Stack is not (yet) defined in binarynode.

the issue is not easily to resolve cause both are template classes with inline implementation. normally you would have forward declaration of BinaryNode in header and include the binarynode.h in cpp file only.  but for templates you have no cpp.

i would say Stack is the more basic class and should not have functions where a BinaryNode was used as member or argument.

Sara
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

763 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now