+ 1
How to call one function which has a function as a parameter in main()?
Example We define a function: string month_convert(string month2, string month_fun(int& month)){}. And now I don't know how to call this function in main. Does anybody has any idea? https://code.sololearn.com/cS3W2egl3CEN/?ref=app https://code.sololearn.com/cuW1bgV4jC3J/?ref=app
6 odpowiedzi
+ 3
Martin Taylor Why would the declaration for month_convert() be invalid? In C++, I think the following are equivalent, at least when specifying a function as parameter:
string month_fun( int& )
string ( *month_fun )( int& )
string ( &month_fun )( int& )
Then again, I usually use template parameters for callables. I found it suggested here:
https://ncona.com/2019/05/passing-functions-as-arguments-in-cpp/
Whether the nested arguments are named should have no bearing, since the parameter would be adjusted to a function pointer either way. If anything, the following code compiles on SoloLearn:
https://code.sololearn.com/c17A226a10A2/?ref=app
If I am wrong please correct me.
+ 2
Well,you can use function pointers
if you are trying to pass a function as an argument to a function in C/C++...??
That's all I understood by your problem...
+ 1
Ok, I will give you the whole code and then you see what can be done.
0
Sorry I haven't come so far. I still have not using template and foo, so I don't know these items yet. Please take one step bsck.
0
What I don't know here is how to use a reference sign & in main function when I am calling a function.
0
I hope I will soon make it corect. But first I more study.