我一直在争夺这个并且环顾四周,但我不确定我做错了什么
错误:
错误:在'。'标记
之前预期的primary-expression正在弹出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);
}
}
这是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 :(得分:2)
您尝试使用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);
}
运算符。例如:
->
注意我也改为{{1}},因为你到处都有指针。