+ 1

[SOLVED] Error in declaring a pointer to a method

I have declared a lambda that, I want to declare a pointer to a method of a class in it. It calls a method dynamically to determine reading from or writing to a binary file. Here is the code, run it to see errors: [EDIT: The code is in the correct form without any errors] https://code.sololearn.com/cs5FVf2UF75j Any helps? Thanks in advance.

30th Mar 2018, 9:35 AM
$machitgarha
$machitgarha - avatar
6 odpowiedzi
+ 1
Special thanks to you, Kinshuk Vasisht, someone helped me and the problem solved. The fixed code link is in the post.
31st Mar 2018, 11:55 AM
$machitgarha
$machitgarha - avatar
+ 3
Use this version of editFile : auto editFile = [&](fstream &binFile, char operation) { // Method pointer basic_iostream<char> (*pFun) (const char *, streamsize); /// Correct way of declaring function pointers. // Checks which operation to do /// Assignment requires typecasting of basic_ostream and /// basic_istream returning functions to basic_iostream if (operation == 'r') pFun = reinterpret_cast<decltype(pFun)>(&binFile.write); else if (operation == 'w') pFun = reinterpret_cast<decltype(pFun)>(&binFile.read); else throw exception(); // Make operations on the file pFun(model, MODEL_SIZE); pFun(company, CO_SIZE); pFun(reinterpret_cast<char *>(&year), sizeof(year)); pFun(reinterpret_cast<char *>(&kmSpent), sizeof(kmSpent)); };
30th Mar 2018, 12:00 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
Can you share the complete code, if possible?
30th Mar 2018, 10:15 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
I put it in the post; but with some editions. Thanks for your help.
30th Mar 2018, 11:04 AM
$machitgarha
$machitgarha - avatar
+ 1
Thank you very very much! It generates two warnings, is it possible to remove them? The code stops working when reaches the first code after 'Make operations on the file'. What should I do? I think they are related to those warnings.
30th Mar 2018, 7:55 PM
$machitgarha
$machitgarha - avatar
0
Ill try to remove them. If possible, can you share a sample file data that I can use to test the code?
31st Mar 2018, 3:00 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar