为什么不编译带有函数指针的代码?
Why doesn't this code with a function pointers compile?
我想传递一个函数指针作为参数。
类定义标头
class Networking : public QObject
{
Q_OBJECT
public:
explicit Networking(QObject *parent = 0);
QNetworkAccessManager* manager;
private:
QUrl buildCall(QString method, QMap<QString, QString> parameters);
public slots:
void reply(QNetworkReply* reply, void(*ptr)(const void *)); // error here
};
在类源中
Networking::Networking(QObject *parent) : QObject(parent)
{
this->manager = new QNetworkAccessManager(this);
}
void Networking::reply(QNetworkReply* reply, void(*ptr)(const void *)) // error here
{
//some code
}
编译收到错误:在"void"处解析错误
命令:
/usr/lib/qt/bin/moc -DQT_WEBKITWIDGETS_LIB -DQT_WEBKIT_LIB -DQT_WIDGETS_LIB -DQT_NETWORK_LIB -DQT_GUI_LIB -DQT_CORE_LIB -I/usr/lib/qt/mkspecs/linux-g++ -I/home/dev/cpp/net-client -I/usr/include/qt -I/usr/include/qt/QtWebKitWidgets -I/usr/include/qt/QtWebKit -I/usr/include/qt/QtWidgets -I/usr/include/qt/QtNetwork -I/usr/include/qt/QtGui -I/usr/include/qt/QtCore -I. -I/usr/include/c++/4.9.1 -I/usr/include/c++/4.9.1/x86_64-unknown-linux-gnu -I/usr/include/c++/4.9.1/backward -I/usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.1/include -I/usr/local/include -I/usr/lib/gcc/x86_64-unknown-linux-gnu/4.9.1/include-fixed -I/usr/include ../project/Networking.h -o moc_apihandler.cpp
我尝试使用,但收到相同的错误
typedef void (*ptr)(const void*);
文档说:
函数指针不能是信号或插槽参数
建议的解决方法是使用 typedef
或函数对象 - 您自己的层次结构,或者如果可以,std::function
/boost::function
。
您发出的命令不是C++编译,而是 Qt 元对象编译器 (moc)。 moc是一个预处理器,它将Qt C++扩展转换为普通C++以进行适当的编译。
错误消息指示void
不是预期的,并且假设它引用函数返回类型并且与函数指针无关,则错误很可能在前面的紧随行或语句中(不包括空格和注释) - 需要更多上下文才能确切地查看内容。 如果前面的行是包含文件,则需要查看包含文件的末尾。 常见错误包括缺少分号和包含文件末尾缺少换行符。
相关文章:
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何防止C++遗留代码中的挂起指针
- 理解这行C++指针代码
- 为什么我们不在下面给出的代码中使用指针来实例化C++的实体对象?
- 为什么我的代码在指针方面停止运行?
- 以下与指针相关的代码的输出是什么?
- C++指针问题:如何修复这些代码中的错误?
- 如何使用指针直接从托管代码中的非托管代码中读取矢量数据
- 为什么下面的代码段返回指针指向的值而不是指针的地址?
- 使用指针向量(带代码)C++(以及对其使用的便利性的怀疑)时出现问题
- 为什么从 constexpr 引用生成的程序集代码与从 constexpr 指针生成的程序集代码不同?
- 由于指针算法错误,代码在 memcpy 中崩溃
- 为什么通过定义另一个指针单元格,整个代码停止工作?
- 使用指针编译代码后,.cpp文件将变为随机字符
- OpenCV如何处理Mat作为指针来加速代码
- 更正代码c++指针并删除
- C++指向对象的指针堆数组中,如何将字符串传递给特定对象?有一些代码请看一下:
- C++ 被此代码与多态性、指针和对象切片混淆
- LLvm Jit:如何从抖动代码中取消引用运行时指针?
- 将 C 代码移植到C++,将 void* 从 malloc 转换为所需指针的问题