-1

这是代码:

QEventLoop eventLoop;
QNetworkAccessManager mgr();
QObject::connect(mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
QUrl url(site);
QNetworkRequest req(url);
QNetworkReply *reply = mgr.get(req);
eventLoop.exec();

if (reply->error() == QNetworkReply::NoError) {
    cout << "Success" endl;
    delete reply;
}
else {
    cout << "Failure" endl;
    delete reply;
}

发现错误:

.. \ request.cpp (17): error C2665: 'QObject :: connect': 3 个重载都不能转换各种主题

..\request.cpp (20): error C2228: '.get' 左边的元素必须是类、结构或联合

4

2 回答 2

3

这被称为最令人头疼的 parse,编译器认为这mgr是一个函数声明。要解决此问题,只需更改

QNetworkAccessManager mgr();

QNetworkAccessManager mgr;

您的 中也有错误connect,它应该如下所示(注意&before mgr,因为connect需要指针):

QObject::connect(&mgr, SIGNAL(finished(QNetworkReply*)), &eventLoop, SLOT(quit()));
于 2015-08-20T11:37:50.877 回答
1

你有额外的括号。利用:

QNetworkAccessManager mgr;

QNetworkAccessManager没有没有参数的构造函数,所以解析器像函数声明一样理解它。此外,您可能会在第二行收到警告,例如:

: warning: empty parentheses interpreted as a function declaration [-Wvexing-parse]
QNetworkAccessManager mgr();
                         ^~
于 2015-08-20T11:40:49.183 回答