### binary tree postfix calculator

Hey guys, me again. This time around, I'm working on a binary tree calculator. Unfortunately, although my algorithms are most likely correct. it seems my grasp of c++ has failed me, and am getting compile errors like crazy, and am at my wits end as to what's wrong-which is a lot.
my code is below. can anyone assist me in spotting any glaring errors? Thanks!
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174`` ``````#include "TreeCalc.h" #include using namespace std; //Constructor TreeCalc::TreeCalc() { stack myStack; } //Destructor- frees memory TreeCalc::~TreeCalc() { cleanTree(myStack->top()); } //Deletes tree/frees memory void TreeCalc::cleanTree(TreeNode* ptr) { if(ptr->value!=NULL){ cleanTree(ptr->left); cleanTree(ptr->right); } else delete ptr; } //Gets data from user void TreeCalc::readInput() { string response; cout << "Enter elements one by one in postfix notation" << endl << "Any non-numeric or non-operator character," << " e.g. #, will terminate input" << endl; cout << "Enter first element: "; cin >> response; //while input is legal while (isdigit(response[0]) || response[0]=='/' || response[0]=='*' || response[0]=='-' || response[0]=='+' ) { insert(response); cout << "Enter next element: "; cin >> response; } } //Puts value in tree stack void TreeCalc::insert(const string& val) { if(isdigit(val)){ TreeNode* t=new TreeNode(val); myStack->push(t); } else{ TreeNode* w=new TreeNode(val); w->right=myStack->top(); myStack->pop(); w->left=myStack->top(); myStack->pop(); myStack->push(w); } } //Prints data in prefix form void TreeCalc::printPrefix(TreeNode* ptr) const { if(ptr!=NULL){ cout<value; printPrefix(ptr->left); printPrefix(ptr->right); } //Prints data in infix form void TreeCalc::printInfix(TreeNode* ptr) const { if(ptr!=NULL){ if(ptr->value=='+'||ptr->value=='-'||ptr->value=='*'||ptr->value=='/'){ cout<value; else { cout<<'('; printInfix(ptr->left); cout<value; printInfix(ptr->right); cout<')'; } } } //Prints data in postfix form void TreeCalc::printPostfix(TreeNode* ptr) const { if(ptr!=NULL){ printPostfix(ptr->left); printPostfix(ptr->right); cout<value; } } // Prints tree in all 3 (pre,in,post) forms void TreeCalc::printOutput() const { if (mystack->size()!=0 && mystack->top()!=NULL) { cout << "Expression tree in postfix expression: "; // call your implementation of printPostfix() printPostfix(myStack->top()); cout << endl; cout << "Expression tree in infix expression: "; // call your implementation of printInfix() printInfix(myStack->top()); cout << endl; cout << "Expression tree in prefix expression: "; // call your implementation of printPrefix() printPrefix(myStack->top()); cout << endl; } else cout<< "Size is 0." << endl; } //Evaluates tree, returns value // private calculate() method int TreeCalc::calculate(TreeNode* ptr) const { if(ptr->left==NULL&&ptr->right==NULL) return atoi((ptr->value).c_str()); else{ int ret=0; switch(ptr->value){ case '+': int l=calculate(ptr->left); int r=calculate(ptr->right); ret=l+r; break; case '-': int l=calculate(ptr->left); int r=calculate(ptr->right); ret=l-r; break; case '*': int l=calculate(ptr->left); int r=calculate(ptr->right); ret=l*r; break; case '/': int l=calculate(ptr->left); int r=calculate(ptr->right); ret=l/r; break; } return ret; } } //Calls calculate, sets the stack back to a blank stack // public calculate() method. Hides private data from user int TreeCalc::calculate() { int i = 0; // call private calculate method here i=calculate(myStack->top()); return i; }``````
 ``12345678910111213141516171819`` ``````#ifndef TREENODE_H #define TREENODE_H #include using namespace std; class TreeNode { public: TreeNode(); //Default Constructor TreeNode(const string & val); //Constructor private: string value; TreeNode *left, *right; // for trees friend class TreeCalc; //gives TreeCalc access to private data }; #endif ``````
 ``1234567891011121314151617181920212223242526272829303132`` ``````#ifndef TREECALC_H #define TREECALC_H #include #include "TreeNode.h" using namespace std; class TreeCalc { public: TreeCalc(); //Constructor ~TreeCalc(); //Destructor void cleanTree(TreeNode * ptr); //Deletes tree/frees memory void readInput(); //gets data from user void insert(const string & val); //puts value in tree // print methods void printPrefix(TreeNode * curNode) const; //prints data in prefix form void printInfix(TreeNode * curNode) const; //prints data in infix form void printPostfix(TreeNode * curNode) const;//prints data in postfix form void printOutput() const; //prints in pre,in,post form int calculate(); //calls private calculate method private: stack myStack; int calculate(TreeNode* ptr) const; //Evaluates tree, returns value }; #endif ``````
 ``1234567891011121314151617`` ``````#include "TreeNode.h" //Default Constructor -left and right are NULL, value '?' TreeNode::TreeNode() { value="?"; left=NULL; right=NULL; } //Constructor - sets value to val TreeNode::TreeNode(const string & val) { value=val; left=NULL; right=NULL; }``````

please note that TreeNode.cpp is not to be altered, and neither is readInput() in TreeCalc.cpp.
What compile errors?

Jim
the full list is as follows:
 ``1234567891011121314151617181920212223242526272829303132333435363738394041`` ``````TreeCalc.cpp: In destructor ‘TreeCalc::~TreeCalc()’: TreeCalc.cpp:19:20: error: base operand of ‘->’ has non-pointer type ‘std::stack’ TreeCalc.cpp: In member function ‘void TreeCalc::cleanTree(TreeNode*)’: TreeCalc.cpp:25:18: error: no match for ‘operator!=’ in ‘ptr->TreeNode::value != 0’ TreeCalc.cpp:25:18: note: candidates are: /usr/include/c++/4.6/bits/stl_pair.h:214:5: note: template bool std::operator!=(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&) /usr/include/c++/4.6/bits/stl_iterator.h:297:5: note: template bool std::operator!=(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&) /usr/include/c++/4.6/bits/stl_iterator.h:347:5: note: template bool std::operator!=(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&) /usr/include/c++/4.6/bits/allocator.h:132:5: note: template bool std::operator!=(const std::allocator<_T1>&, const std::allocator<_T2>&) /usr/include/c++/4.6/bits/allocator.h:137:5: note: template bool std::operator!=(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&) /usr/include/c++/4.6/bits/stl_deque.h:259:5: note: template bool std::operator!=(const std::_Deque_iterator<_Tp, _Ref, _Ptr>&, const std::_Deque_iterator<_Tp, _Ref, _Ptr>&) /usr/include/c++/4.6/bits/stl_deque.h:266:5: note: template bool std::operator!=(const std::_Deque_iterator<_Tp, _RefL, _PtrL>&, const std::_Deque_iterator<_Tp, _RefR, _PtrR>&) /usr/include/c++/4.6/bits/stl_deque.h:1943:5: note: template bool std::operator!=(const std::deque<_Tp, _Alloc>&, const std::deque<_Tp, _Alloc>&) /usr/include/c++/4.6/bits/stl_stack.h:265:5: note: template bool std::operator!=(const std::stack<_Tp, _Seq>&, const std::stack<_Tp, _Seq>&) /usr/include/c++/4.6/bits/postypes.h:223:5: note: template bool std::operator!=(const std::fpos<_StateT>&, const std::fpos<_StateT>&) /usr/include/c++/4.6/bits/basic_string.h:2473:5: note: template bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&) /usr/include/c++/4.6/bits/basic_string.h:2485:5: note: template bool std::operator!=(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&) /usr/include/c++/4.6/bits/basic_string.h:2497:5: note: template bool std::operator!=(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) /usr/include/c++/4.6/bits/streambuf_iterator.h:200:5: note: template bool std::operator!=(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&) /usr/include/c++/4.6/ext/new_allocator.h:128:5: note: template bool __gnu_cxx::operator!=(const __gnu_cxx::new_allocator<_Tp>&, const __gnu_cxx::new_allocator<_Tp>&) /usr/include/c++/4.6/bits/stl_iterator.h:817:5: note: template bool __gnu_cxx::operator!=(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&) /usr/include/c++/4.6/bits/stl_iterator.h:811:5: note: template bool __gnu_cxx::operator!=(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&) TreeCalc.cpp: In member function ‘void TreeCalc::insert(const string&)’: TreeCalc.cpp:56:17: error: no matching function for call to ‘isdigit(const string&)’ TreeCalc.cpp:56:17: note: candidates are: /usr/include/ctype.h:115:1: note: int isdigit(int) /usr/include/ctype.h:115:1: note: no known conversion for argument 1 from ‘const string {aka const std::basic_string}’ to ‘int’ /usr/include/c++/4.6/bits/locale_facets.h:2566:5: note: template bool std::isdigit(_CharT, const std::locale&) TreeCalc.cpp:57:32: error: conversion from ‘TreeNode*’ to non-scalar type ‘TreeNode’ requested TreeCalc.cpp:58:12: error: base operand of ‘->’ has non-pointer type ‘std::stack’ TreeCalc.cpp:61:32: error: conversion from ‘TreeNode*’ to non-scalar type ‘TreeNode’ requested TreeCalc.cpp:62:6: error: base operand of ‘->’ has non-pointer type ‘TreeNode’ TreeCalc.cpp:62:21: error: base operand of ‘->’ has non-pointer type ‘std::stack’ TreeCalc.cpp:63:12: error: base operand of ‘->’ has non-pointer type ‘std::stack’ TreeCalc.cpp:64:6: error: base operand of ‘->’ has non-pointer type ‘TreeNode’ TreeCalc.cpp:64:20: error: base operand of ‘->’ has non-pointer type ‘std::stack’ TreeCalc.cpp:65:12: error: base operand of ‘->’ has non-pointer type ‘std::stack’ TreeCalc.cpp:66:12: error: base operand of ‘->’ has non-pointer type ‘std::stack’ TreeCalc.cpp: In member function ‘void TreeCalc::printPrefix(TreeNode*) const’: TreeCalc.cpp:82:1: error: a function-definition is not allowed here before ‘{’ token TreeCalc.cpp:177:1: error: expected ‘}’ at end of input``````
The first error says that in the TreeCalc::~TreeCalc destructor you're trying the use the pointer dereference operator '->' on something that's not a pointer.

If we look, you have

 `` `` ``cleanTree(myStack->top());``

Following to the declaration of myStack, we find that it's an actual object, not a pointer to an object. So, that code needs to be

 `` `` ``cleanTree(myStack.top());``

I see you've done throughout the code, so fix those up and then move on to the next error.

Which is saying that in TreeCalc::cleanTree it doesn't know how to compare TreeNode::value with zero.

 `` `` ``if(ptr->value!=NULL){``

If we look at the definition of TreeNode::value, it's a string object. You can't compare a string object with a number.

Don't get confused by what's returned when you dereference a pointer.
This code - `ptr->value` dereferences the `ptr` pointer and gets the value of the member called `value`. The actual value - which in this case is a string.

If you're using an empty string to mean "not-initialised" then you can probably just compare the string to "", rather than NULL (which is just another name for zero)

Fix those problems and your error list will be a bit shorter.

Jim
thanks man, this helped a lot.
No worries
Topic archived. No new replies allowed.