2

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!

Tyler B.
  • 31
  • 5
  • Have a look at std:function, a function wrapper. – Damien Nov 11 '18 at 21:22
  • can you post the signature of the member function inorderTraverse? Your compiler is complaining that it is not finding a function inorderTraverse which takes the argument you supplied... – user3726947 Nov 11 '18 at 21:44
  • *so there must be a way to make it work.* -- Yes there is. The problem isn't that there is a way, the problem is that the way to do this may "violate" what your assignment can use. – PaulMcKenzie Nov 11 '18 at 21:58

1 Answers1

1

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.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • This all makes sense to me. My prof got back to me as well, and said pretty much the same thing, that studioTest should not be a member function. I'm working on fixing the code now to verify that it was the problem. Very interesting link about std::bind as well. I've not heard of that before, and will have to do some reading when I can about that. – Tyler B. Nov 12 '18 at 00:40