0

I am making a Binary Search Tree and I have the following function in a .cpp file:

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

}
  • What doesn't work actually? – πάντα ῥεῖ Oct 11 '16 at 19:35
  • You need an instance of `MovieTree` and you call functions on it. That is why it is called Object Oriented Programming. – NathanOliver Oct 11 '16 at 19:37
  • @πάντα_ῥεῖ It produces an error whenever I attempt to reference the instances of MovieTree –  Oct 11 '16 at 20:09
  • Crack the book time, Whatamia. This is fundamental syntax that will be covered quite early in any beginner C++ text that isn't an outright fraud. On the off chance your text is a fraud, [check out some of these.](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – user4581301 Oct 11 '16 at 20:19
  • Please read ["What is a “static” function?"](http://stackoverflow.com/questions/558122/what-is-a-static-function) – Nikita Oct 11 '16 at 20:22

1 Answers1

0

You need an instance of MovieTree. Somehwere in your code and above the switch statement, you should add:

MovieTree movieTree;

and then your switch statement will look like this:

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.

David Peterson
  • 728
  • 6
  • 17
  • Marking it static would tell the compiler it doesn't need an instance to be called. Thus, you could call it like you attempted originally `MovieTree::printMovieInventory(node)`. The downside is static functions can't access class state (instanced class state anyway) since there is no instance tied to the call. If you are never going to add fields or instance methods, though, it is an option. – David Peterson Oct 11 '16 at 20:26