2014-02-16 78 views
0

我一直在解決這個了一會兒,環顧四周,但我不知道我在做什麼錯'。'之前的預期主表達式令牌

錯誤:

錯誤:「」之前預期基本表達式令牌

被彈出了大部分的addElement方法 其中BinaryNode.variable而言內部的代碼,但我完全失去了在這裏做什麼

#include <cstdlib> 
#include "BinarySearchTree.h" 
using namespace std; 

template <typename Comparable> 
BinarySearchTree<Comparable>::BinarySearchTree(const Comparable & theElement, BinarySearchTree<Comparable> *leftTree, 
BinarySearchTree<Comparable> *rightTree) : BinaryNode(theElement,leftTree,rightTree) { 
} 

template <typename Comparable> 
void BinarySearchTree<Comparable>::addElement(Comparable newElement) { 
    if(newElement < BinaryNode.element) { 
     if(BinaryNode.left == NULL) { 
      BinaryNode.left = BinarySearchTree(newElement, NULL, NULL); 
      BinaryNode.right.root = BinaryNode; 
     } else { 
      BinaryNode.left.addElement(newElement); 
     } 
    } else if (newElement > BinaryNode.element) { 
     if(BinaryNode.right == NULL) { 
      BinaryNode.right = BinarySearchTree(newElement, NULL, NULL); 
      BinaryNode.right.root = this; 
     } else { 
      BinaryNode.right.addElement(newElement); 
     } 
    } 

而這裏的heade對於BinarySearchTree

#include <vector> 
using namespace std; 

template<typename Comparable> 
class BinarySearchTree { 
public: 
    BinarySearchTree(const Comparable & theElement, BinarySearchTree<Comparable> * leftTree, 
     BinarySearchTree<Comparable> * rightTree); 
    void addElement(Comparable newElement); 
    void removeElement(Comparable newElement); 
    BinarySearchTree<Comparable> * findElement(Comparable newElement); 
    bool isEmpty(); 
    BinarySearchTree & operator=(const BinarySearchTree &tree); 
    vector<BinarySearchTree> preOrder(vector<BinarySearchTree> * list); 
    vector<BinarySearchTree> inOrder(); 
    vector<BinarySearchTree> postOrder(); 

private: 
    struct BinaryNode { 
     Comparable element; 
     BinarySearchTree<Comparable> *left; 
     BinarySearchTree<Comparable> *right; 

     BinaryNode(const Comparable & theElement, BinarySearchTree<Comparable> *leftTree, 
     BinarySearchTree<Comparable> *rightTree) : element(theElement), left(leftTree), right(rightTree){} 
    }; 

    BinaryNode *root; 
}; 
+0

請指出錯誤發生在哪一行。 – Brian

+0

這是代碼爲 – user3317055

回答

2

R檔你正在嘗試使用BinaryNode作爲變量名,但它是一個類型。您不能在一個類型上,僅在一個對象上使用.運算符。例如:

if(newElement < root->element) { 
    if(root->left == NULL) { 
     root->left = BinarySearchTree(newElement, NULL, NULL); 
     root->right->root = new BinaryNode; 
    } else { 
     root->left->addElement(newElement); 
    } 

通知我改->爲好,因爲你有一個指針無處不在,太。

+0

中的每個BinaryNode引用根將是父級BinaryNode。我將如何去獲取當前類中的BinaryNode結構中的元素變量?我沒有使用過多的結構,在我的任務中它是不可編輯的 – user3317055

+0

在理解設計的期望方面,我犯了一個錯誤,感謝您的幫助。 – user3317055

相關問題