Tyler B.
Tyler B.

Reputation: 31

C++ Cannot pass function to Binary Search tree traverse method

I am working on a school project and have run into a serious roadblock. The project involves making a database using a Binary Search Tree (BST provided for us), which I have done. I am now trying to write search methods. The solution encouraged by the professor is to code:

filmDatabaseBST.inorderTraverse(studioTest);

where filmDatabaseBST is the database (containing Film objects), and studioTest is the method being passed.

void FilmDatabase::studioTest(Film& anItem)
{
  string s;   //temporary variable to hold studio from Film
  s = anItem.getStudio(); //
  if (s == findStudio)  //if the search term and the Films studio match 
  {
     Film tempFilm = filmDatabaseBST.getEntry(anItem);  //copy Film object and display the data
     display(tempFilm);
     searchResult = true; //set check to true to indicate a result was found
  }

studioTest() is currently a public method within the class. When I attempt this is get the following error:

FilmDatabase.cpp:209:49: error: no matching function for call to 'BinarySearchTree<Film>::inorderTraverse(<unresolved overloaded function type>)'

I should add that using

filmDatabase.inorderTraverse(display);

works perfectly calling the following method at each node and printing the data from the object stored there.

void display(Film& anItem)
{
  anItem.printFilm();
}

The only difference is display() is not defined in the .h file, and not assigned the FilmDatabase:: namespace. I've attempted similar with studioTest() but I get the same errors.

Can anyone give me a push in right direction? I'm fairly new as a programmer and with C++, and this is my first post so I apologize if crucial information is missing. Please let me know if that is the case and I'll provide/explain whatever I can.

Note: I've done as much research as I can but all results I've found are issues with the BST code itself, which I've been repeatedly assured is clean and I'm not supposed to alter. Other students have been successful in implementing our professors advice, so there must be a way to make it work.

EDIT: The sum of my problem was that they needed to be free functions, as kindly pointed out by Zan Lynx in the answer below. After altering the program to allow for that, it all works perfectly. Thank you very much for the help!

Upvotes: 2

Views: 303

Answers (1)

Zan Lynx
Zan Lynx

Reputation: 54325

I believe that your issue is that display is a free function and studioTest is a member function.

Calling a member function requires a special kind of function pointer that must combine a pointer to an instance of the object and a pointer to the member function within the object.

A free function or a class static function is much easier to use.

Also check this out: How std::bind works with member functions

You may be able to use std::bind. It depends on the function signature of inOrderTraverse. If all it looks for is a "Callable" as a template then it would work.

Otherwise you will not be able to use a member function without strange work-arounds like a global object pointer for a free function to use when calling the member function on the global object. Very hacky and not a good idea.

Upvotes: 1

Related Questions