Why this code is not compiled?



  • I try many ways to make this code works, but I don't know to fix it to work, or find another to do the same thing...
    Explanations?
    I will still reading to find the soluction! Thanks all!

    ProtocoloFactory.h
    @
    #ifndef PROTOCOLOFACTORY_H
    #define PROTOCOLOFACTORY_H

    #include <QObject>
    #include <bo/protocolo.h>

    class ProtocoloFactory : public QObject
    {
    Q_OBJECT
    public:
    explicit ProtocoloFactory(QObject *parent = 0);

    Protocolo createLerHistoricoComando();
    

    signals:

    public slots:

    };

    #endif // PROTOCOLOFACTORY_H
    @

    ProtocoloFactory.cpp
    @
    #include "protocolofactory.h"
    #include "bo/protocolo.h"

    ProtocoloFactory::ProtocoloFactory(QObject *parent) :
    QObject(parent)
    {
    }

    Protocolo ProtocoloFactory::createLerHistoricoComando()
    {
    Protocolo retorno(0);
    retorno.setOpcode(6);
    return retorno;
    }
    @



  • What is your compiler output error?

    And why do you declare empty signal and slots section? If you don't use this, you don't need to declare these section.



  • QtCreator Auto-Generates this section!

    Compiler error:
    .../InoveBEV/bo/protocolofactory.h:5: In file included from ../InoveBEV/bo/protocolofactory.h:5:0,
    .../InoveBEV/bo/protocolofactory.cpp:1: from ../InoveBEV/bo/protocolofactory.cpp:1:
    .../bin/QtSDK/Desktop/Qt/4.8.1/gcc/include/QtCore/qobject.h:-1: In copy constructor 'Protocolo::Protocolo(const Protocolo&)':
    .../bin/QtSDK/Desktop/Qt/4.8.1/gcc/include/QtCore/qobject.h:333: error: 'QObject::QObject(const QObject&)' is private
    .../InoveBEV/bo/protocolo.h:7: error: within this context
    .../InoveBEV/bo/protocolofactory.cpp:-1: In member function 'Protocolo ProtocoloFactory::createLerHistoricoComando()':
    .../InoveBEV/bo/protocolofactory.cpp:13: synthesized method 'Protocolo::Protocolo(const Protocolo&)' first required here


  • Moderators

    If Protocolo is a QObject, then your ProtocoloFactory::createLerHistoricoComando() method will not work, as it's relying on returning a copy of the Protocolo object created in line 11. QObjects are non-copyable by their nature.



  • I that case I will need use Reference (*)?
    Or Cannot be done what I want?!


  • Moderators

    You would need to use new to create the object on the heap and return a pointer to it, ideally a smart pointer.



  • Thanks mlong, I will do that and study the necessary.

    Thanks.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.