Need help calling function | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Need help calling function

here is the InOrderPrint private recursive function template<class T> void BinaryTree<T>::InOrderPrint(BinNode<T>* cursor, void process(T& data)) { if(cursor != nullptr) { InOrderPrint(cursor->left, process); process(cursor->data); InOrderPrint(cursor->right, process); } } Here is the public function that calls it. template<class T> void BinaryTree<T>::InOrderPrint(void process(T *data)) { InOrderPrint(root, process); //call to private function using a Node object //called root and the parameter process as an //argument. } inside main is where I call the function after instantiating the Binary tree object. I have also declared a function called process in main to see if I could pass the void function as an argument, however it has not been defined yet. void process(Card* card); cards.InOrderPrint(process(*cardInfo)); // here is where the error is flagged and states: void process(Card* card) { //not defined yet }

5th Jun 2021, 3:09 AM
Blake Thompson
3 Answers
+ 2
Blake Thompson The first and the most apparent error is that you are passing the argument to `cards.InOrderPrint` incorrectly. The method requires a function as argument, but here, you calling process() with the argument `*cardinfo` and then passing its return value to `cards.InOrderPrint`. In other words, you are passing the result of `process(*cardinfo)` to`cards.InOrderPrint`, which is invalid as process() is declared as void and does not return anything. To pass the function process() to `cards.InOrderPrint`, you need to do `cards.InOrderPrint(process);` Another error is that in the public `BinaryTree<T>::InOrderPrint`, you are taking a function of the type `void (T*)` (function that takes a T pointer and returns nothing). But you are passing it to the private `BinaryTree<T>::InOrderPrint` method that takes a function of type `void (T&)` (function thst takes a T reference and returns nothing) If there are any more errors, I suggest you save your code in the code playground and link it here.
7th Jun 2021, 6:12 AM
XXX
XXX - avatar
+ 1
You mean the template arguments or the method arguments? The template argument is only 1 and as long as the method is not static, you wouldn't need to pass it while calling the method, as it would've already been passed during the initialization of the object The method argument is a function that takes a pointer to type T and returns nothing. So a function of the pattern similar to void func_name(int* arg){...} Can be passed to it.
5th Jun 2021, 6:40 AM
XXX
XXX - avatar
+ 1
You'll need to show the error message and the code. Without that, no one can say anything
5th Jun 2021, 8:06 PM
XXX
XXX - avatar