Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Problem on linker.



  • Hello, I am a noob programmer and I am also new with QT. I have a problem on my code and I don't know how to resolve. Please help.

    Main.cpp

    #include <QCoreApplication>
    #include <QDebug>
    #include "persona.h"
    int main(int argc, char *argv[])
    {
        QCoreApplication a(argc, argv);
        QObject *p = new QObject;
        Persona* Juan = new Persona(p);
        Juan->setNombre("Juan");
    
        qDebug() << Juan;
        
        delete p;
        return a.exec();
    }
    --------------------------------------------------------------
    
    

    persona.h

    -------------------------------------------------------------
    #ifndef PERSONA_H
    #define PERSONA_H
    
    #include <QObject>
    
    class Persona : public QObject
    {
        Q_OBJECT
    public:
        explicit Persona(QObject *parent = nullptr);
    
        void setNombre(const QString &nombre);
    
        void habla(const QString &palabra);
    
        friend inline QDebug operator<<(QDebug qd, Persona *p);
    
    signals:
        void hablo(QString);
    public slots:
        void escucha(const QString &palabra);
    private:
        QString m_nombre;
    };
    
    #endif // PERSONA_H
    ---------------------------------------------------
    

    persona.cpp

    #include "persona.h"
    #include <QDebug>
    
    Persona::Persona(QObject *parent) : QObject(parent)
    {
    
    }
    
    void Persona::setNombre(const QString &nombre){
        m_nombre = nombre;
    }
    
    void Persona::habla(const QString &palabra){
        qDebug() << m_nombre << "Dice " << palabra;
        emit hablo(palabra);
    }
    
    void Persona::escucha(const QString &palabra){
       qDebug() << m_nombre << "Ha escuchado" << palabra;
    }
    
    inline QDebug operator<<(QDebug qd,  Persona *p){
        return qd << "El nombre es: " << p->m_nombre;
    }
    

    problems:
    main.obj:-1: error: LNK2019: unresolved external symbol "class QDebug __cdecl operator<<(class QDebug,class Persona *)"
    debug\newoperator.exe:-1: error: LNK1120:

    Thanks


  • Qt Champions 2019

    remove the inline from the operator since it's not inline.




Log in to reply