Issue with QNetworkAccessManager Singleton Injection

133 views Asked by At

I'm writing a RSS Feed Reader in Qt. My approach is to save every Feed in an object of the class Feed. The download is handled inside the Feed class, therefore I need to make sure that every object of Feed can access the (one) NetworkAccessManager. To manage this (and to make sure the NetworkAccessManager has only one instance) I want to make a Singleton injection. The problem is, with my current code I get the following error message

...main.cpp:-1: Fehler: undefined reference to `NetworkMgr::qnam'

Following is the code of which I think it is the most relevant for the problem, if you need anything more let me know.

I try to get the pointer on the instance of the NetworkAccessManager directly inside the constructor of the Feed class and then safe it in a private variable.

#ifndef FEED_H
#define FEED_H

#include <QObject>
#include <QGuiApplication>
#include <QtQuick>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
#include <QPixmap>
#include <iostream>
#include <QDomDocument>
#include "NetworkMgr.h"

using namespace std;

class Feed : public QObject {

    Q_OBJECT

    Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
    Q_PROPERTY(QString url READ url WRITE setUrl NOTIFY urlChanged)
    Q_PROPERTY(int id READ id WRITE setId NOTIFY idChanged)
    Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged)

public:
    Feed(QObject* parent = NULL) : QObject(parent){

        m_nMgr = NetworkMgr::getInstance();
        connect(m_nMgr, SIGNAL(finished(QNetworkReply*)), this, SLOT(parse(QNetworkReply*)));
        m_active = false;

    };

    QString name();
    void setName(QString);
    QString url();
    void setUrl(QString);
    int id();
    void setId(int);
    bool active();
    void setActive(bool);

public slots:
    void get();

private:
    QString m_name;
    QString m_url;
    int m_id;
    bool m_active;
    QNetworkAccessManager* m_nMgr;

private slots:
    void parse(QNetworkReply* reply);

signals:
    void nameChanged();
    void urlChanged();
    void idChanged();
    void activeChanged();
};

#endif

This is my Singleton class:

#ifndef NETWORKMGR_H
#define NETWORKMGR_H

#include <QObject>
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>

class NetworkMgr : public QObject {
    Q_OBJECT

    NetworkMgr(QObject* parent = NULL) : QObject(parent){};
    ~NetworkMgr(){
        delete qnam;
    }
    static QNetworkAccessManager* qnam;

public:
    static QNetworkAccessManager* getInstance(){
        if(qnam == NULL){
            qnam = new QNetworkAccessManager();
        }
        return qnam;
    }

    NetworkMgr(NetworkMgr const& copy) = delete;
    NetworkMgr& operator=(NetworkMgr const& copy) = delete;

};

//QNetworkAccessManager* NetworkMgr::qnam = NULL;

#endif

As you can see, I also tried to initialize qnam to nullpointer, but then I get the error:

...Feed.h:17: Fehler: multiple definition of NetworkMgr::qnam' debug/main.o: In function Feed::~Feed()': ...Feed.h:17: multiple definition of `NetworkMgr::qnam'

And this is how I tried to call it as a test from main.cpp:

    Feed feed;
    feed.setUrl("https://www.deskmodder.de/blog/feed/");
    feed.get();

I tried getting the instance of NetworkAccessManager outside the constructor as well, but the error stays the same.

I would really appreciate it, if anybody here has an idea what I could try to get rid of this error.. Before I take a completely different approach, I'd like to try it this way.

Thanks in advance to anyone contributing to fix my problem!

0

There are 0 answers