我正在尝试将我的课程的函数void write_log(String verbosity, Variant message);
传递给另一个将进行线程事件循环(ev.h)的课程。 String和Variant来自godot命名空间。
我已经使用typedef使用定义typedef void (godot::MQTT::*Logger)(godot::String, godot::Variant);
使write_log成为Logger类型。此定义存储在被调用者类中,而不是在调用类中。
被调用的函数定义为void initialize(Logger logger);
,然后用下面的代码调用。
libumqtt::Client client;
client.initialize(&MQTT::write_log);
我的主叫类别为godot::MQTT
,被叫类别为libumqtt::Client
。
当我尝试使用write_log时发生错误。
void Client::initialize(Logger write_log) { ... write_log("error", "Initializing MQTT Client!!! TypeDEF!!!"); ... }
错误消息如下。
➜ 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.
我一直在试图弄清为什么我不能让编译器识别函数指针,但是到目前为止还没有这样的运气。我收到了一些错误,例如在弄乱我的代码时无法使用间接寻址等。
我还使用这些链接来帮助我学习如何使用typedef并传递函数指针:
我正在尝试通过我的课程的函数void write_log(String verbosity,Variant message);到另一个将成为线程事件循环(ev.h)的类。 String和Variant来自godot命名空间。 ...
从您发布的有限代码段来看,似乎没有任何错误。但是,如果您在此函数调用之前的行中错过了分号等,则已知gcc / g ++编译器会引发此错误。