'.' 之前需要主表达式令牌

编程入门 行业动态 更新时间:2024-10-25 17:23:59
本文介绍了'.' 之前需要主表达式令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

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

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 method where 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.

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

更多推荐

[db:关键词]

本文发布于:2023-05-01 06:30:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1405867.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:令牌   表达式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!