2014-02-16 42 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

相关问题