I am trying to pass my class' function void write_log(String verbosity, Variant message);
to another class which will be threading event loop (ev.h). String and Variant are from the godot namespace.
I have used typedef to make write_log into type Logger using the definition typedef void (godot::MQTT::*Logger)(godot::String, godot::Variant);
. This definition is stored in the callee class and not the calling class.
The function being called is defined as void initialize(Logger logger);
which is then called with the code below.
libumqtt::Client client;
client.initialize(&MQTT::write_log);
My calling class is godot::MQTT
and the callee class is libumqtt::Client
.
My error occurs when I try to use write_log.
void Client::initialize(Logger write_log) {
...
write_log("error", "Initializing MQTT Client!!! TypeDEF!!!");
...
}
The error message is as below.
➜ Godot-MQTT-Module git:(master) ✗ scons platform=osx bits=64
scons: Reading SConscript files ...
scons: done reading SConscript files.
scons: Building targets ...
g++ -o src/MQTT.os -c -g -O2 -arch x86_64 -std=c++17 -fPIC -I. -Igodot-cpp/godot_headers -Igodot-cpp/include -Igodot-cpp/include/core -Igodot-cpp/include/gen -Isrc -Ilibraries/libumqtt/src src/MQTT.cpp
g++ -o src/client.os -c -g -O2 -arch x86_64 -std=c++17 -fPIC -I. -Igodot-cpp/godot_headers -Igodot-cpp/include -Igodot-cpp/include/core -Igodot-cpp/include/gen -Isrc -Ilibraries/libumqtt/src src/client.cpp
src/client.cpp:120:14: error: called object type 'libumqtt::Client::Logger' (aka 'void (godot::MQTT::*)(godot::String, godot::Variant)') is not a function or function pointer
write_log("error", "Initializing MQTT Client!!! TypeDEF!!!");
~~~~~~~~~^
1 error generated.
scons: *** [src/client.os] Error 1
scons: building terminated because of errors.
I have been trying to figure out why I cannot get the compiler to recognize the function pointer, but have had no such luck so far. I've received errors such as not being able to use indirection and so on while messing around with my code.
I also used these links to help me with learning how to use typedef and pass a function pointer:
- Typedef Fully Qualified Name With Pointers - http://www.radmangames.com/programming/how-to-use-function-pointers-in-cplusplus
- Using vs Typedef - https://www.internalpointers.com/post/differences-between-using-and-typedef-modern-c
- Typedef in Header - How do I refer to a typedef in a header file?
- Typedef Examples - https://en.wikipedia.org/w/index.php?title=Typedef&oldid=923023181#Function_pointers
- What is Typedef - https://www.cprogramming.com/tutorial/typedef.html
- Pass A Function Pointer - https://stackoverflow.com/a/9413/6828099
Edit: On uninitialized's mention of my code looking correct, but the amount posted is limited, I am pasting the links to the classes and header files from my repo. These links are marked for this specific commit, so the content won't change even when I update the code. Also, this library is MIT, so you won't have to worry about copyright.
Edit 2: Using this answer, I have checked the type of variable I am trying to use write_log
in the callee class and it returns what I expected it to. So, I have no idea why the compiler cannot see that this is a function. I am using C++17 to compile my code and C++14 has the same issue (C++11 breaks on other code before even getting to this, so it doesn't provide useful results).
void (godot::MQTT::*)(godot::String, godot::Variant)