gpt4 book ai didi

c++ - AVL搜索树

转载 作者:行者123 更新时间:2023-11-30 05:44:51 26 4
gpt4 key购买 nike

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <fstream>
#include <string>
using namespace std;

class Node
{
public:
string word;
Node *left;
Node *right;
Node *parent;
int number;
int balance;
int height;
Node(string x);
Node(string x, Node *nleft, Node *nright);
Node(string, Node *nparent, Node *nleft, Node *nright);
void setWord(string x);
string getWord();
void setParent(Node *nparent);
Node getParent();
void setLeft(Node *nleft);
Node getLeft();
void setRight(Node *nright);
Node getRight();
void setNumber(int num);
int getNumber();
};
Node::Node(string x){
word = x;
parent = NULL;
left = NULL;
right = NULL;
number = 1;
balance = 0;
height = 0;

}
Node::Node(string x, Node *nleft, Node *nright){
word = x;
parent = NULL;
left = nleft;
right = nright;
number = 1;
balance = 0;
height = 0;

}
Node::Node(string x, Node *nparent, Node *nleft, Node *nright){
word = x;
parent = nparent;
left = nleft;
right = nright;
number = 1;
balance = 0;
height = 0;
}
void Node::setWord(string x){
word = x;
}
string Node::getWord(){
return word;
}
void Node::setParent(Node *nparent){
parent = nparent;
}
Node Node::getParent(){
return parent;
}
void Node::setLeft(Node *nleft){
left = nleft;
}
Node::Node getLeft(){
return left;
}
void Node::setRight(Node *nright){
right = nright;
}
Node::Node getRight(){
return right;
}
void Node::setNumber(int num){
number = num;
}
int Node::getNumber(){
return number;
}

我正在尝试在 eclipse 中的 AVL 树中创建我的节点类,我想知道为什么在控制台中返回错误 no viable conversion from 'Node *' to 'Node' for my getters。解决和解决此问题的最佳原因是什么。

最佳答案

你的 getter 应该返回指针(另外作为一个提示,普通的 getter 通常应该是 const)

Node* Node::getParent() const
{
return parent;
}

按照目前的编写,该函数被声明为按值返回一个 Node,但是您的 getter 返回一个指针,这就是错误告诉您的内容。

关于c++ - AVL搜索树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29592420/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com