Answered by:
error c2143 missing ; before <

Hello,
I keep getting the frustrating compilation error given in the title when I try to compile the following code:
#ifndef binarySearchTree_ #define binarySearchTree_ #include "linkedBinaryTree (1).h" #include "bsTree.h" using namespace std; template<class K, class E> class binarySearchTree : public linkedBinaryTree<pair<const K, E> >, public bsTree<K,E> { public: // methods of dictionary bool empty() const {return this>treeSize == 0;} int size() const {return this>treeSize;} pair<const K, E>* find(const K& theKey) const; void insert(const pair<const K, E>& thePair); void erase(const K& theKey); // additional method of bsTree void ascend() {this>inOrderOutput();} }; #endif
This is the basic definition for a binary search tree class. I've included all the necessary files in the project, and I cannot find anywhere where I'm missing a semicolon. What could be causing this error?Thanks,
 Robin
Question
Answers

 Proposed as answer by Helen Zhao Friday, November 18, 2011 1:31 AM
 Marked as answer by rob05cl Sunday, November 20, 2011 6:26 PM
All replies


rob05cl wrote:
This is the basic definition for a binary search tree class. I've included all the necessary files in the project, and I cannot
find anywhere where I'm missing a semicolon. What could be causing this error?The error message is most likely misleading. My guess is, the compiler sees an angle bracket < following an identifer that is not known to the compiler to be the name of a template, so it must be a lessthan operator. And since the operator is not allowed in that part of the grammar, the compiler tries to recover as best it can, and assumes that you meant to terminate the statement and start a new one.
So look either for a misspelled identifier, or for a template being used before it was declared.
Igor Tandetnik

Thanks Igor,
I've gotten almost everything to work but I still have one last error when I compile. Here's my new code:
// linked binary tree implementation of a binary search tree // implements all dictionary and bsTree methods #ifndef binarySearchTree_ #define binarySearchTree_ #include "linkedBinaryTree.h" #include "bsTree.h" using namespace std; template<class K, class E> class binarySearchTree : public linkedBinaryTree<pair<const K, E> >, public bsTree<K,E> { public: // methods of dictionary bool empty() const {return this>treeSize == 0;} int size() const {return this>treeSize;} pair<const K, E>* find(const K& theKey) const; void insert(const pair<const K, E>& thePair); void erase(const K& theKey); // additional method of bsTree void ascend() {this>inOrderOutput();} class inorderIterator { public: //tracking pointer binaryTreeNode<pair<K, E> > *p1, *checked, *root, *rtrn; //stack stack<binaryTreeNode<pair<K, E> >*> myStack; bool stackStarted; //constructor inorderIterator(binaryTreeNode<pair<K, E> > *root){ checked = NULL; rtrn = NULL; stackStarted = false; p1 = root; this>root = root; //push first element if(p1 != NULL){ myStack.push(p1); } else{ cout << "empty tree...terminating" << endl; } } /****************************** HASNEXT() ******************************/ bool hasNext() { //cout << "hasnext: " << !(checked == root) << endl; return !(myStack.empty()); } /***************************** NEXT() *****************************/ binaryTreeNode<pair<K, E> >* next(){ //loop while a node hasn't been returned while(true){ bool stop = false; //add nodes to stack while they have left children and left child hasn't been checked while(!stackStarted){ if(p1>leftChild == NULL){ //return node stackStarted = true; } else{ p1 = p1>leftChild; myStack.push(p1); } } //if element is leaf, pop it if(p1>rightChild == NULL){ rtrn = myStack.top(); myStack.pop(); if(!myStack.empty()){ p1 = myStack.top(); } else{ return rtrn; } return rtrn; } else{ rtrn = p1; p1 = p1>rightChild; myStack.pop(); myStack.push(p1); stackStarted = false; return rtrn; } } } }; inorderIterator* beginIO () { inorderIterator *i = new inorderIterator(root); return i; } }; template<class K, class E> pair<const K, E>* binarySearchTree<K,E>::find(const K& theKey) const {// Return pointer to matching pair. // Return NULL if no matching pair. // p starts at the root and moves through // the tree looking for an element with key theKey binaryTreeNode<pair<const K, E> > *p = this>root; while (p != NULL) // examine p>element if (theKey < p>element.first) p = p>leftChild; else if (theKey > p>element.first) p = p>rightChild; else // found matching pair return &p>element; // no matching pair return NULL; } template<class K, class E> void binarySearchTree<K,E>::insert(const pair<const K, E>& thePair) {// Insert thePair into the tree. Overwrite existing // pair, if any, with same key. // find place to insert binaryTreeNode<pair<const K, E> > *p = this>root, *pp = NULL; while (p != NULL) {// examine p>element pp = p; // move p to a child if (thePair.first < p>element.first) p = p>leftChild; else if (thePair.first > p>element.first) p = p>rightChild; else {// replace old value p>element.second = thePair.second; return; } } // get a node for thePair and attach to pp binaryTreeNode<pair<const K, E> > *newNode = new binaryTreeNode<pair<const K, E> > (thePair); if (this>root != NULL) // the tree is not empty if (thePair.first < pp>element.first) pp>leftChild = newNode; else pp>rightChild = newNode; else this>root = newNode; // insertion into empty tree this>treeSize++; } template<class K, class E> void binarySearchTree<K,E>::erase(const K& theKey) {// Delete the pair, if any, whose key equals theKey. // search for node with key theKey binaryTreeNode<pair<const K, E> > *p = this>root, *pp = NULL; while (p != NULL && p>element.first != theKey) {// move to a child of p pp = p; if (theKey < p>element.first) p = p>leftChild; else p = p>rightChild; } if (p == NULL) return; // no pair with key theKey // restructure tree // handle case when p has two children if (p>leftChild != NULL && p>rightChild != NULL) {// two children // convert to zero or one child case // find largest element in left subtree of p binaryTreeNode<pair<const K, E> > *s = p>leftChild, *ps = p; // parent of s while (s>rightChild != NULL) {// move to larger element ps = s; s = s>rightChild; } // move largest from s to p, can't do a simple move // p>element = s>element as key is const binaryTreeNode<pair<const K, E> > *q = new binaryTreeNode<pair<const K, E> > (s>element, p>leftChild, p>rightChild); if (pp == NULL) this>root = q; else if (p == pp>leftChild) pp>leftChild = q; else pp>rightChild = q; if (ps == p) pp = q; else pp = ps; delete p; p = s; } // p has at most one child // save child pointer in c binaryTreeNode<pair<const K, E> > *c; if (p>leftChild != NULL) c = p>leftChild; else c = p>rightChild; // delete p if (p == this>root) this>root = c; else {// is p left or right child of pp? if (p == pp>leftChild) pp>leftChild = c; else pp>rightChild = c; } this>treeSize; delete p; } // overload << for pair template <class K, class E> ostream& operator<<(ostream& out, const pair<K, E>& x) {out << x.first << ' ' << x.second; return out;} #endif
// test linked binary tree class #include <iostream> #include <fstream> #include "binarySearchTree.h" #include "linkedBinaryTree.h" using namespace std; int main(void) { linkedBinaryTree<int> a,x,y,z; y.makeTree(1,a,a); z.makeTree(2,a,a); x.makeTree(3,y,z); y.makeTree(4,x,a); cout << "Number of nodes = "; cout << y.size() << endl; cout << "height = "; cout << y.height() << endl; cout << "Preorder sequence is "; y.preOrderOutput(); cout << "Inorder sequence is "; y.inOrderOutput(); cout << "Postorder sequence is "; y.postOrderOutput(); cout << "Level order sequence is "; y.levelOrderOutput(); /////////////////// test for iterator //POSTORDER ITERATOR linkedBinaryTree<int>::postorderIterator *ip = y.beginPO() ; while(ip>hasNext()) y.output(ip>next()) ; //in order iterator of bst binarySearchTree<const int, string> bst; ifstream in("prob2in.txt"); if(in.is_open()){ //read #lines int lines; in >> lines; cout << "lines: " << lines << endl; int key; string value; while(lines > 0){ //retrieve pairs and insert them into bst in >> key; in >> value; cout << "key: " << key << endl; cout << "value: " << value << endl; bst.insert(pair<const int, string>(key, value)); lines; cout << "lines: " << lines << endl; } cout << "root: " << bst.rootElement()>first << bst.rootElement()>second; } // //INORDER ITERATOR cout << "my in order iterator" << endl; binarySearchTree<const int, string>::inorderIterator *ii = bst.beginIO() ; while(ii>hasNext()){ bst.output(ii>next().element); } return 0; }
Basically the issue is with the last part of my cpp file, starting where it says "// //INORDER ITERATOR". I'm creating an iterator for the binary search tree class, which will traverse the tree in order. However, I am getting an error on the following line:bst.output(ii>next().element);
and I'm getting a red squiggle under the pointer "ii". Here's the error message:
2 IntelliSense: expression must have class type 70 14 hw4prob2
Error 1 error C2228: left of '.element' must have class/struct/union 70 1 hw4prob2What do you think could be causing the problem?Thanks, Robin 
 Proposed as answer by Helen Zhao Friday, November 18, 2011 1:31 AM
 Marked as answer by rob05cl Sunday, November 20, 2011 6:26 PM
