本文介绍了'.' 之前需要主表达式令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为此挣扎了一段时间并环顾四周,但我不确定我做错了什么

I've been battling this for a while and looking around but I'm not sure what I'm doing wrong

错误:

错误:'.' 标记前的预期主表达式

error: expected primary-expression before ‘.’ token

弹出 addElement 方法中的大部分代码关注 BinaryNode.variable 但我完全不知道在这里做什么

is popping up for most of the code inside the addElement methodwhere BinaryNode.variable is concerned but I'm completely lost on what to do here

#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 的头文件

And here's the header file for 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;
 };

推荐答案

您正在尝试使用 BinaryNode 作为变量名,但它是一种类型.您不能在类型上使用 . 运算符,只能在对象上使用.例如:

You're trying to use BinaryNode as a variable name, but it's a type. You can't use the . operator on a type, just on an object. For example:

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

请注意,我也更改为 ->,因为您也到处都有指针.

Notice I changed to -> as well, since you have pointers everywhere, too.

这篇关于'.' 之前需要主表达式令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 06:16