问题描述
我正在Qt中为REST API创建一种路由器,并且在将函数指针插入QMap时遇到了问题.
I am creating sort of a router for REST API in Qt and I am facing problem with inserting the function pointer into the QMap.
我有一个IModule
类,从中派生了其他模块.IModule.h
的重要行是
I have class IModule
from which the other modules are derivated.Important lines of IModule.h
are
typedef QByteArray* (*IBusAction)(IBus * , ProxyRequest *);
class IModule : public QObject
{
Q_OBJECT
protected:
QMap<QString , IBusAction > *m_actions;
然后我有一个UserModule
,它是从IModule
派生的,在.cpp
文件中,我有以下几行:
Then I have UserModule
which is derived from IModule
and in .cpp
file I have these lines:
QByteArray* UserModule::create(IBus *bus, ProxyRequest *req)
{
}
QByteArray* UserModule::show( IBus *bus, ProxyRequest *req)
{
}
UserModule::UserModule(QObject *parent) :
IModule(parent)
{
// register functions
m_actions->insert("show", &UserModule::show);
m_actions->insert("create", UserModule::create);
}
因此,我尝试了两个选项,如何在没有引用的情况下将具有引用符号的函数放入QMap中,但两者均无法正常工作.我收到错误消息:no matching function for call to 'QMap<QString, QByteArray* (*)(IBus*, ProxyRequest*)>::insert(const char [5], QByteArray* (UserModule::*)(IBus*, ProxyRequest*))'
So I tried two options how to put the function in QMap with referencing sign also without it, but both are not working. I am getting error: no matching function for call to 'QMap<QString, QByteArray* (*)(IBus*, ProxyRequest*)>::insert(const char [5], QByteArray* (UserModule::*)(IBus*, ProxyRequest*))'
我花了几个小时解决这个问题,尝试了许多不同的方法来解决它,但是没有成功.
I have spent several hours with this problem, tried many different things how to solve it but there was no success.
因此,我将很高兴为您提供任何建议.
So I will be very glad for any piece of advice.
推荐答案
您的IBusAction
是指向函数的指针类型.这与指向成员的指针功能不兼容.
Your IBusAction
is a pointer-to-function type. This is not compatible with pointer-to-member function.
当调用成员函数(如UserModule::create
函数)时,它需要一个额外的(不可见")参数:调用该函数的实例(this
指针).
When you call a member function (like your UserModule::create
function), it needs an extra ("invisible") parameter: the instance the function gets called on (the this
pointer).
您基本上有三个选择:
-
将
IBusAction
更改为UserModule
成员函数的指针.但是,您只能使用该类的功能.
Change
IBusAction
to be a pointer-to-member-function-of-UserModule
. You're restricted to that class's functions with this though.
typedef QByteArray* (UserModule::*IBusAction)(IBus * , ProxyRequest *);
使函数static
(这将更改代码的语义)
Make the functions static
(this changes the semantics of your code)
这篇关于将函数指针插入QMap(Qt)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!