how to pass functions as parameters to another function
-
wrote on 6 Jan 2019, 12:02 last edited by
I want to pass function to another and this is my code:
.h file:void Read_Func(QModbusDataUnit::RegisterType reg_type, int Start_Address, int Number_of_Entries, int SlaveID, void (*f)());
.cpp file:
void settings::Read_Func(QModbusDataUnit::RegisterType reg_type, int Start_Address, int Number_of_Entries, int SlaveID, void (*f)()) { }
ERROR:
error: [release/moc_settings.cpp] Error 1
-
Hi
The function pointer syntax seems fine.
The error is something else in the .h file ( i would guess)
That error means that the moc tool didnt make moc_settings.cpp -
Hi
The function pointer syntax seems fine.
The error is something else in the .h file ( i would guess)
That error means that the moc tool didnt make moc_settings.cpp -
Is Read_Func listed as a slot ?
I just tried same syntax here and it compiled. so its not syntax it self, i think.
-
Is Read_Func listed as a slot ?
I just tried same syntax here and it compiled. so its not syntax it self, i think.
-
@rezaMSLM
Just tested. got same error.
So moc does not support functions pointers for slot parameters it seems.
http://doc.qt.io/qt-5/moc.html#function-pointers-cannot-be-signal-or-slot-parameters
but you can use the typedef workaround. -
@rezaMSLM
Just tested. got same error.
So moc does not support functions pointers for slot parameters it seems.
http://doc.qt.io/qt-5/moc.html#function-pointers-cannot-be-signal-or-slot-parameters
but you can use the typedef workaround.@mrjj typedefs makes working with function pointers easier, anyway. Thats why I prefer them too.
-
@mrjj typedefs makes working with function pointers easier, anyway. Thats why I prefer them too.
@aha_1980
Indeed, a typedef makes the code easier to read but also
allow to change the function signature in one place.
So the its a win-win in this case :) -
wrote on 6 Jan 2019, 13:17 last edited by
I used typedef as mentioned in that link
here is the result:
.h file:typedef void (*f)(); class settings : public QDialog { . . private slots: void Read_Func(QModbusDataUnit::RegisterType reg_type, int Start_Address, int Number_of_Entries, int SlaveID, f); . . void test(); }
.cpp:
void settings::on_pushButton_Minute_Read_clicked() { Read_Func(QModbusDataUnit::HoldingRegisters, 3, 1,CorrectID,test);//<--error } void settings::Read_Func(QModbusDataUnit::RegisterType reg_type, int Start_Address, int Number_of_Entries, int SlaveID, f) { auto ReadUnit = QModbusDataUnit(reg_type, Start_Address, Number_of_Entries); if(auto *reply = ModbusObject->sendReadRequest(ReadUnit, SlaveID)) { if(!reply->isFinished()) connect(reply,&QModbusReply::finished,[this](){f();}); else{ //statusBar()->showMessage("error!;Reply Deleted",2000); delete reply; } } else ; // statusBar()->showMessage("error!",2000); } void settings::test() { qDebug()<<"test executed"; }
ERROR:
no matching function for call to 'settings::Read_Func(QModbusDataUnit::RegisterType, int, int, int&, <unresolved overloaded function type>)' Read_Func(QModbusDataUnit::HoldingRegisters, 3, 1,CorrectID,test); ^
-
Lifetime Qt Championwrote on 6 Jan 2019, 13:31 last edited by mrjj 1 Jun 2019, 13:33
Hi
you have test() as part of a class and hence its not a c function pointer as your
declaration suggest. ( the typedef)
So either move it outside class or make it static.
or use a member function pointer so that the info needed to call it is included.
https://isocpp.org/wiki/faq/pointers-to-members -
wrote on 7 Jan 2019, 05:39 last edited byThis post is deleted!
1/11