从类中传递基础构造函数?

时间:2016-10-12 01:16:54

标签: c++ debugging object binary-search-tree

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

    }

这是二进制搜索树的代码。 我和我的&#34; addMovieNode&#34;有一些问题。功能。它永远不会进入我初始化BST的第一个节点的初始情况。 我在很大程度上怀疑是因为我误解了如何在我的主要内容中传递第一个构造函数&#39;驱动程序。

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

main.cpp中的引用如下:

 MovieTree movieTree;

 MovieTree();


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

我在通过函数传递值后得到分段错误,它从不进入初始的if(root)语句

1 个答案:

答案 0 :(得分:0)

addMovieNode函数中,您声明了一个局部变量root

MovieNode* root = new MovieNode;

这是隐藏变量root,它是该类的成员。

修复方法是删除声明,以便修改类成员:

root = new MovieNode;

你的构造函数中也发生了同样的事情:

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

还需要将其更改为root = NULL;