C++ Header and Declaration Nested struct and class syntax - c++

So I'm trying to define a header file and declaration file with the appropriate code. In my class I am using a struct and then using the structs members to initialize other functions. I am not sure how to do the pathing to link the class and struct to the appropriate function and was hoping someone could help.
This is my header file here.
class AVL {
public:
struct node {
int data;
node* left;
node* right;
int height;
};
node* root;
node* insert(int key, node* tree);
node* oneright(node*& tree);
node* oneleft(node*& tree);
node* twoleft(node*& tree);
node* tworight(node*& tree);
int height(node* tree);
int getBalance(node* tree);
int preorder(node* tree);
AVL();
node* insert(int key);
void parsetree();
vector<int> vvector;
vector<int> hvector;
vector<int> bfvector;
};
The main problem I am having is how to declare it in the .cpp file
here
For example, if I wanted to do insert function how would I properly do the syntax. For that I have
node* AVL::node::insert(int key, node* tree)
But that is throwing me an error and I haven't been able to find anything when searching for it. Thank you!

In AVL.h
class AVL {
node* root;
node* insert(int key, node* root);
node* insert(int key);
};
in AVL.cpp
AVL::node* AVL::insert(int key, AVL::node* root) {
//code here...
}
AVL::node* AVL::insert(int key) {
//code here...
}
As it is mention in one the comments, the insert is not a member of the node struct.

Related

What does Node * head outside of the struct do?

So I have the class LinkedList. Can someone explain why node * head is outside of the struct and node * next, prev are inside the struct? What is the purpose of this and how does it differ from the others? thanks
class LinkedList
{
public:
LinkedList();
~LinkedList();
void add(char ch);
bool find(char ch);
bool del(char ch);
void display(
private:
struct node
{
char data;
node * next;
node * prev;
};
node *head;
};
This
struct node
{
char data;
node * next;
node * prev;
};
is an inner declaration of a structure type within a class.
This
node *head;
is a declaration of a data member of the type struct node * of the class LinkedList. To make it more clear rewrite the list definition the following way
struct node
{
char data;
node * next;
node * prev;
};
class LinkedList
{
public:
LinkedList();
~LinkedList();
void add(char ch);
bool find(char ch);
bool del(char ch);
void display(
private:
node *head;
};
Declaring the structure inside the class definition as a private declaration makes the structure type invisible for the users of the linked list.

Program throws error when I increment a private static variable in C++

I created a private static variable that keeps track of the number of elements in the linked list.
struct node
{
int data;
node *next;
};
class linkedList
{
private:
node *head,*tail;
static int listSize;
public:
linkedList()
{
head=NULL;
tail=NULL;
}
void insert(int n)
{
node *temp=new node;
temp->data=n;
temp->next=NULL;
if(head == NULL)
{
head=temp;
tail=temp;
}
else
{
tail->next=temp;
tail=temp;
}
linkedList::listSize+=1;
}
};
void main()
{
linkedList l;
l.insert(10);
l.insert(20);
}
The compiler throws an error when it reaches the line linkedList::listSize+=1;
error: ‘linkedList’ has not been declared.
Once your typos corrected (inser(20) instead of insert(20) and : instead of ; in linkedList(), your program almost compiles.
There is just one thing missing: you need to implement the listSize variable somewhere for example by putting int linkedList::listSize; before main:
...
int linkedList::listSize; /(/ <<< add this
void main()
{
linkedList l;
l.insert(10);
l.insert(20);
}
But why are you using a static variable for counting the elements of the list? You probably want listSize to be an ordinary (non static) class member, just as head and tail:
class linkedList
{
private:
node * head, *tail;
int listSize; // no static
public:
...
and drop the int linkedList::listSize; suggested before.

Compilation errors in resolving public functions in C++

I'm trying a classic programming interview problem. The idea is to create a balanced binary tree(or a tree with minimum height) from a sorted array. This is my node class.
class node{
public:
node(int data):value(data), left(nullptr), right(nullptr){}
node* sortedArrayToBinaryTree(int arr[], int start, int end){
if(start > end) return nullptr;
int mid = (start + end)/2;
node* p = new node(arr[mid]);
p->left = sortedArrayToBinaryTree(arr, start, mid-1);
p->right = sortedArrayToBinaryTree(arr, mid+1, end);
return p;
}
void preorder(node* root){
if(root == nullptr) return;
std::cout<<root->value<<" "<<std::endl;
preorder(root->left);
preorder(root->right);
}
private:
int value;
node* left;
node* right;
};
I'm reasonably sure that my logic is ok. However the issue is when I write a client code to test the functionality my public methods in node class are not being resolved.
int main() {
int arr[] = {2,7,9,13,19,21};
node* root = sortedArrayToBinaryTree();
preorder();
return 0;
}
I get the following compilation error.
error: use of undeclared identifier 'sortedArrayToBinaryTree'
node* root = sortedArrayToBinaryTree(arr, 0, 5);
^
main.cpp:10:5: error: use of undeclared identifier 'preorder'
preorder();
^
2 errors generated.
You forgot not only to pass actual arguments to the functions, but the fact, that these non-static member functions (methods) need a class instance to be called on. I don't see any.
Nevertheless, it doesn't make sense to make sortedArrayToBinaryTree and preorder members of node. If you're making a tree, there should be class tree that takes care of the sorting/ordering etc... So, logic is not OK.
Function sortedArrayToBinaryTree is a non-static member function of class node.
It may not be called withput an object of this class.
You could declare it as a static data member in the class definition
static node* sortedArrayToBinaryTree(int arr[], int start, int end){
//...
and in this case you could write
node* root = node::sortedArrayToBinaryTree( /* arguments */ );
And as the function does not have default arguments you have to specify them explicitly.
The same is applied to funcion preorder.
Take into account that these functions do not use data members of the class. So the class design is wrong.
I think you should declare these functions as static member functions of the class.
You are getting compiler errors since the functions sortedArrayToBinaryTree and preorder are declared in node as member functions and you are using them from main as though they are non-member functions.
I can think of the following ways you can resolve the problem.
Make the functions non-member functions
Declare the functions.
node* sortedArrayToBinaryTree(int arr[], int start, int end);
void preorder(node* root);
Make them friends of node.
class node{
public:
node(int data):value(data), left(nullptr), right(nullptr){}
friend node* sortedArrayToBinaryTree(int arr[], int start, int end);
friend void preorder(node* root);
private:
int value;
node* left;
node* right;
};
And then use them from main, with right arguments.
int main() {
int arr[] = {2,7,9,13,19,21};
node* root = sortedArrayToBinaryTree(arr, 0, sizeof(arr)/sizeof(arr[0]));
preorder(root);
return 0;
}
Make the functions static member functions
class node{
public:
node(int data):value(data), left(nullptr), right(nullptr){}
static node* sortedArrayToBinaryTree(int arr[], int start, int end);
static void preorder(node* root);
private:
int value;
node* left;
node* right;
};
and call them from main.
int main() {
int arr[] = {2,7,9,13,19,21};
node* root = node::sortedArrayToBinaryTree(arr, 0, sizeof(arr)/sizeof(arr[0]));
node::preorder(root);
return 0;
}

error: '(Class object)' does not name a type

I have a header file containing this class definition:
class visitorlist {
struct Node {
visitor vis;
Node* next;
};
Node* head;
Node* tail;
public:
visitorlist() { //written here to have it as inline.
head = NULL;
tail= NULL;
}
~visitorlist();
int lengthvl();
void add(const visitor);
void popandexit();
void transfer(visitorlist);
void deletenode(Node*);
int refiprio();
int refioffno();
int refifloor();
visitor reravi();
bool isempty();
Node* rehead();
};
and in a source file with the above header included I have:
Node* visitorlist::rehead() {
return head;
}
This causes an error: 'Node' does not name a type.
Isn't Node on the scope of the function?
Use
visitorlist::Node* visitorlist::rehead() {
return head;
}
Or, since C++11:
auto visitorlist::rehead() -> Node* {
return head;
}

C++ Binary Tree, modifying root node, pointer to pointer to root node

I realize the title isn't too descriptive so here are the details. I'm implementing my own Binary Tree class in C++. I have written a template Node class and template Binary Tree class already, for the most part, and am stuck on something. I created an empty binary tree (root node is null) and when I try to set that node it fails miserably. here is the code and more explanation:
template<class T> class Node
{
T _key;
Node<T> *_leftChild;
Node<T> *_rightChild;
public:
Node();
Node(T key);
Node(T key, Node<T> *leftChild, Node<T> *rightChild);
~Node();
bool hasLeftChild();
bool hasRightChild();
void setKey(T key);
void setLeftChild(Node<T> *node);
void setRightChild(Node<T> *node);
T getKey();
Node<T>* getLeftChild();
Node<T>* getRightChild();
bool compare(Node<T> *compareNode); // return true if this.Node < compareNode
};
Node implementation not really necessary.. ( I dont think ) it's quite long.
#include "Node.cpp"
#include <iostream>
using namespace std;
template<class T> class BinaryTree
{
Node<T> *_root;
public:
BinaryTree();
BinaryTree(Node<T> *root);
~BinaryTree();
Node<T>* getRoot();
void insert(Node<T> **root, Node<T> *node);
};
template<class T>
BinaryTree<T>::BinaryTree()
{
this->_root = NULL;
}
template<class T>
BinaryTree<T>::BinaryTree(Node<T> *root)
{
this->_root = root;
}
template<class T>
BinaryTree<T>::~BinaryTree()
{
// delete stuff
}
template<class T>
Node<T>* BinaryTree<T>::getRoot()
{
return this->_root;
}
template<class T>
void BinaryTree<T>::insert(Node<T> **root, Node<T> *node)
{
if(!*root)
{
*root = node;
}
}
Main:
BinaryTree<int> *tree = new BinaryTree<int>();
Node<int> *root = tree->getRoot();
Node<int> **root1 = &root;
cout << tree->getRoot() << endl;
Node<int> *noChildrenNode = new Node<int>(2);
tree->insert(&root1, noChildrenNode);
cout << tree->getRoot() << endl;
Inserts current functionality is just supposed to replace the NULL root pointer to the node pointer passed in as a parameter. The failing miserably part is since the pointer is a copy it isn't actually setting the root node.. but I can't seem to figure out how to set up a pointer to a pointer to the root node so it can be altered.. I've got to be close and any help will be MUCH appreciated.
Thanks
First, you've got to include the exact text of any error messages. "fails miserably" is not adequate.
I think you want
root = node;
Not
*root = node;
Because if root is null, using *root is a null pointer exception.