简体   繁体   English

在BST中使用C ++使用目标文件的功能

[英]Using functions of an Object file using C++ in a BST

I am making a Binary Search Tree and I have the following function in a .cpp file: 我正在制作二进制搜索树,并且在.cpp文件中具有以下功能:

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

I am a little unsure as to how I should be referring to this function in my main.cpp file or "driver file". 我不确定应如何在main.cpp文件或“驱动程序文件”中引用此函数。 I referred to it in main using this: 我主要使用它来引用它:

            case 3: //message is read in from file
              {
                MovieTree::printMovieInventory(node);
              }

                break;

However, Upon referencing this it just throws an error: 但是,引用此方法只会引发错误:

Driver.cpp:37:40: error: cannot call member function 'void MovieTree::printMovieInventory(MovieNode*) without object
MovieTree::printMovieInventory(node);

Not sure what this means. 不知道这意味着什么。

full main here: 全部主要在这里:

int main(int argc, char **argv)
{
    bool quit = false;
    string s_input;
    int input;

    // loop until the user quits
    while (!quit)
    {
        MovieNode* node = new MovieNode;
        printOptions();

        // read in input, assuming a number comes in
        getline(cin, s_input);
        input = stoi(s_input);

        switch (input)
        {
            // print all nodes
            case 1:     //rebuild network

                break;


                break;

            case 3: //message is read in from file
              {
                MovieTree::printMovieInventory(node);
              }
                break;

            case 4:     // quit
                quit = true;
                cout << "Goodbye!"<< endl;
                break;

            default:    // invalid input
                cout << "Invalid Input" << endl;
                break;
        }
    }

}

You need an instance of MovieTree. 您需要一个MovieTree实例。 Somehwere in your code and above the switch statement, you should add: 在您的代码中和switch语句上方,应该添加:

MovieTree movieTree;

and then your switch statement will look like this: 然后您的switch语句将如下所示:

case 3: //message is read in from file
                movieTree.printMovieInventory(node);
                break;

Alternatively, if MovieTree has no state (ie. no fields or other functions it depends on) you can mark printMoviewTree as static instead of the code above. 另外,如果MovieTree没有状态(即没有它依赖的字段或其他功能),则可以将printMoviewTree标记为static而不是上面的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM