user123
user123

Reputation: 25

AVL search tree

#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;
}

I am attempting to create my node class in my AVL tree in eclipse and I was wondering why the error no viable conversion from 'Node *' to 'Node' for my getters is being returned in the console. What is the best why to address and fix this problem.

Upvotes: 1

Views: 71

Answers (1)

Cory Kramer
Cory Kramer

Reputation: 117856

Your getters should return pointers (also as a tip trivial getters should typically be const)

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

As currently written, the function is declared to return a Node by value, but your getter is returning a pointer, which is what the error is telling you.

Upvotes: 1

Related Questions