+ 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.
6 Respuestas
+ 1
Special thanks to you, Kinshuk Vasisht, someone helped me and the problem solved.
The fixed code link is in the post.
+ 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));
};
+ 1
Can you share the complete code, if possible?
+ 1
I put it in the post; but with some editions.
Thanks for your help.
+ 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.
0
Ill try to remove them. If possible, can you share a sample file data that I can use to test the code?