没有匹配函数来调用 'connect'(QT)

no matching function for call to 'connect'(QT)

我是写QT代码的新手。我写了一个简单的http客户端代码(Qt版本是5.9)。我定义了一个名为 my_http_client 的 class,它派生了 Qobject,我使用 QNetworkAccessManager class 创建 http 连接。我创建了 3 个文件,一个是 main.cpp,另外两个是 my_http_client.hmy_http_client.cpp。在文件 my_http_client.cpp 中定义的 my_http_client class。好的,看流动的是我的代码。当我编译我的项目时,它有错误。

D:\Documents\code\c++\qt_console\http\http_client\my_http_client.cpp:16: error: no matching function for call to 'my_http_client::connect(QNetworkReply*&, const char*, my_http_client*, const char*)'
    QObject::connect(rep_QNetworkReply, SIGNAL(finished()), this, SLOT(httpFinished()));

我不知道发生了什么,我试图解决它,但我不能。我看 same questions。 我的答案没有解决problem.Please帮帮我。我是如何更正 error.Thanks.

my_http_client.h

#ifndef MY_HTTP_CLIENT_H
#define MY_HTTP_CLIENT_H


/*create by victory, time: 2020/05/16
This file define a simple httpclient class,support basic http request
*/

#include <QObject>
#include <QNetworkAccessManager>

QT_BEGIN_NAMESPACE
class QNetworkAccessManager;
class QNetworkReply;
QT_END_NAMESPACE

class my_http_client : public QObject
{
    Q_OBJECT

public:
    explicit my_http_client(QObject *parent = Q_NULLPTR);

    ~my_http_client();      //destructor

public:
    void get(const QUrl &url);


private:
    QNetworkAccessManager *manager;
    QNetworkReply* rep_QNetworkReply;


private slots:
    void httpFinished();
    void httpReadyRead();
};

#endif // MY_HTTP_CLIENT_H

my_http_client.cpp

#include "my_http_client.h"
#include <iostream>


my_http_client::my_http_client(QObject *parent)
    :  QObject(parent)
{
    this->manager = new QNetworkAccessManager(this);
}

//client send get request
void my_http_client::get(const QUrl &url)
{

   this->rep_QNetworkReply = this->manager->get(QNetworkRequest(url));
   connect(rep_QNetworkReply, SIGNAL(finished()), this, SLOT(httpFinished()));

   //connect(rep_QNetworkReply, SIGNAL(readyRead()), this, SLOT(httpReadyRead()));
}

//QNetworkReply send a finished siganl
//function replyFinished recv it and handle it
void my_http_client::httpFinished()
{

    std::cout << "httpFinished:  " << rep_QNetworkReply << std::endl;

    QVariant redirectionTarget = rep_QNetworkReply->attribute(QNetworkRequest::RedirectionTargetAttribute);
    if (rep_QNetworkReply->error())
    {
        std::cerr << "get failed: " << rep_QNetworkReply->errorString() << std::endl;
    }

    rep_QNetworkReply->deleteLater();
    rep_QNetworkReply = Q_NULLPTR;


}



void my_http_client::httpReadyRead()
{
    std::cout << "httpReadyRead: " << std::endl;
    std::cout << rep_QNetworkReply->readAll() << std::endl;

}

my_http_client::~my_http_client()
{
    delete this->manager;
}

main.cpp

#include <QCoreApplication>
#include "my_http_client.h"


/*this is a sample http client, create it by victory*/
/*time is 2020:05:16*/

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    my_http_client pClient;
    pClient.get(QUrl("http://127.0.0.1:8888/index.html"));


    return a.exec();
}

尝试在 my_http_client.h 中添加 #include <QNetworkReply>,它解决了我的问题。

此外,我建议将 std 日志替换为 qDebug()qCritical()#include <QDebug> (debugging in Qt)