Whatamia Whatamia - 1 month ago 12
C++ Question

Passing a base constructor from a class?

void MovieTree::addMovieNode(int ranking, std::string title, int releaseYear, int quantity)
{

std::cout<<"Adding root"<<std::endl;
if(root == NULL)
{
std::cout<<"enter root"<<std::endl;
MovieNode* root = new MovieNode;
root->ranking = ranking;
root->title = title;
root->year = releaseYear;
root->quantity = quantity;
root->leftChild = root->rightChild = NULL;
std::cout<<"Item added"<<std::endl;
std::cout<<title<<std::endl;

}

else if(root)
{
std::cout<<"skipped root"<<std::endl;
MovieNode* newNode = new MovieNode;
newNode->ranking = ranking;
newNode->title = title;
newNode->year = releaseYear;
newNode->quantity = quantity;


MovieNode* currentNode = root;
MovieNode* previousNode = currentNode;

while(currentNode)
{
previousNode = currentNode;

if(currentNode->title.compare(title) > 0)
{
currentNode = currentNode->rightChild;
}
else if(currentNode->title.compare(title) < 0)
{
currentNode = currentNode->leftChild;
}

}

if(previousNode->title.compare(title) > 0)
{
previousNode->rightChild = newNode;
}
else if(previousNode->title.compare(title) <0)
{
previousNode->leftChild = newNode;
}
}

}


This is code for a Binary Search Tree.
I'm having some issues with my "addMovieNode" function. It's never entering the initial case wherein I initialize the first node of the BST.
I suspect in large part that it's due to my misunderstanding of how to pass the first constructor in my 'main.cpp' driver.

#include "MovieTree.hpp"
#include <iostream>

MovieTree::MovieTree()
{
MovieNode* root = NULL;

}

void MovieTree::printMovieInventory()
{
printMovieInventory(root);
}
void MovieTree::printMovieInventory(MovieNode* node)
{
if(node)
{
while(node->rightChild && node->leftChild)
{
std::cout<<"Movie:"<<node->title<<" "<<node->quantity<<std::endl;
if(node->rightChild)
{
printMovieInventory(node->rightChild);
}
if(node->leftChild)
{
printMovieInventory(node->leftChild);
}
}
}
else
{
std::cout<<"No movies in list!"<<std::endl;
}
}

void MovieTree::addMovieNode(int ranking, std::string title, int releaseYear, int quantity)
{

std::cout<<"Adding root"<<std::endl;
if(root == NULL)
{
std::cout<<"enter root"<<std::endl;
MovieNode* root = new MovieNode;
root->ranking = ranking;
root->title = title;
root->year = releaseYear;
root->quantity = quantity;
root->leftChild = root->rightChild = NULL;
std::cout<<"Item added"<<std::endl;
std::cout<<title<<std::endl;

}

else if(root)
{
std::cout<<"skipped root"<<std::endl;
MovieNode* newNode = new MovieNode;
newNode->ranking = ranking;
newNode->title = title;
newNode->year = releaseYear;
newNode->quantity = quantity;


MovieNode* currentNode = root;
MovieNode* previousNode = currentNode;

while(currentNode)
{
previousNode = currentNode;

if(currentNode->title.compare(title) > 0)
{
currentNode = currentNode->rightChild;
}
else if(currentNode->title.compare(title) < 0)
{
currentNode = currentNode->leftChild;
}

}

if(previousNode->title.compare(title) > 0)
{
previousNode->rightChild = newNode;
}
else if(previousNode->title.compare(title) <0)
{
previousNode->leftChild = newNode;
}
}

}


void MovieTree::findMovie(std::string title)
{
if(search(title))
{
MovieNode* foundMovie = search(title);
std::cout<< "Movie Info:"<<std::endl;
std::cout<< "==========="<<std::endl;
std::cout<< "Ranking:" <<foundMovie->ranking<<std::endl;
std::cout<< "Title:" <<foundMovie->title<<std::endl;
std::cout<< "Year:" <<foundMovie->year<<std::endl;
std::cout<< "Quantity:" <<foundMovie->quantity<<std::endl;
}
else
{std::cout<<"Movie not found."<<std::endl;}
}

MovieNode* MovieTree::search(std::string title)
{
if(root)
{
MovieNode* temp = root;
while(temp && temp->rightChild && temp->leftChild)
{
if(!temp)
{
if(temp->title == title)
{
return temp;
}
else if(temp->title.compare(title) > 0)
{
temp = temp->rightChild;
}
else if(temp->title.compare(title) < 0)
{
temp = temp->leftChild;
}
}

}
}
else
{
std::cout<<"Root does not exist! Movie not found!\n";
}
}

void MovieTree::rentMovie(std::string title)
{
if(search(title))
{
MovieNode* foundMovie = search(title);

if(foundMovie->quantity > 0)
{
foundMovie->quantity--;
std::cout<<"Movie has been rented."<<std::endl;
std::cout<<"Movie Info:"<<std::endl;
std::cout<<"==========="<<std::endl;
std::cout<<"Ranking:"<<foundMovie->ranking<<std::endl;
std::cout<<"Title:"<<foundMovie->title<<std::endl;
std::cout<<"Year:"<<foundMovie->year<<std::endl;
std::cout<<"Quantity:"<<foundMovie->quantity<<std::endl;
}
else if(foundMovie->quantity == 0)
{
std::cout<<"Movie out of stock"<<std::endl;
}
}
else if(!search(title))
{
std::cout<<"Movie not found."<<std::endl;
}
}


The reference in the main.cpp is as follows:

MovieTree movieTree;

MovieTree();


movieTree.addMovieNode(12, "The good and bad", 1870, 12);
cout<<"Added"<<endl;


I get a segmentation fault after the values are passed through the function and it never enters the initial if(root) statement

Answer

In the addMovieNode function you're declaring a local variable root:

MovieNode* root = new MovieNode;

This is hiding the variable root that's a member of the class.

The fix is to remove the declaration, so that the class member is modified instead:

root = new MovieNode;

The same thing is happening in your constructor:

MovieTree::MovieTree()
{
    MovieNode* root = NULL;
}

That needs to be changed to simply root = NULL; as well.