[英]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.
我的“ addMovieNode”函数遇到一些问题。 It's never entering the initial case wherein I initialize the first node of the BST.
我从未初始化过初始化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.
我在很大程度上怀疑这是由于我对如何在“ main.cpp”驱动程序中传递第一个构造函数的误解造成的。
#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: main.cpp中的引用如下:
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 值通过函数传递后出现分段错误,并且永远不会输入初始if(root)语句
In the addMovieNode
function you're declaring a local variable root
: 在
addMovieNode
函数中,您声明一个局部变量root
:
MovieNode* root = new MovieNode;
This is hiding the variable root
that's a member of the class. 这隐藏了属于该类成员的变量
root
。
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;
需要将其更改为简单的
root = NULL;
as well. 也一样
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.