我来自Objective-C,很高兴看到内联函数在C++11中出现了,它们非常有用!不幸的是,我在我的插槽中遇到了问题(这个设置让我想起了协议和委托的一点点,我想知道是否可以创建一个类,专门像Obj-C中的“委托”那样行事)。
#include <QCoreApplication>
#include <QtNetwork/QTcpSocket>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QTcpSocket *socket = new QTcpSocket;
socket->connectToHost("qt.nokia.com", 80);
QObject::connect(socket, &QTcpSocket::connected, [socket, page] () {
socket->write(QByteArray("GET " + "index.html" + "\r\n"));
});
QObject::connect(socket, &QTcpSocket::readyRead, [socket] () {
qDebug()<< "GOT DATA "<< socket->readAll();
});
return a.exec();
}
这给我带来了以下错误:
main.cpp:10: error: expected expression
QObject::connect(socket, &QTcpSocket::connected, [socket, page] () {
^