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

Qt - 2 classes use each other



  • Hi,
    I have two classes:
    1 - SerialPort
    2 - SharedClass

    I wanna to use some functions in each class in second class.
    But I have a lot of error like "syntax error: missing ';' before '*'" on line "SharedClass * sharedClass;";

    In fact, I want the "serialport" class contain only simple functions and other classes to handle port data.

    Please guide me;

    1 - SerialPort.cpp

    #include "serialport.h"
    #include <QtSerialPort/QSerialPort>
    #include "sharedclass.h"
    
    SerialPort::SerialPort()
    {
        SharedClass* _sharedClass = new SharedClass;
        sharedClass=_sharedClass;
        serial = new QSerialPort(this);
        ....
    }
    .....
    void SerialPort::sendData(const QByteArray &data)
    {
        ...
    }
    void SerialPort::readData()
    {
        QByteArray data = serial->read(1);
        sharedClass->receiveData(data);
    }
    .....
    ....
    

    SerialPort.h

    #ifndef SERIALPORT_H
    #define SERIALPORT_H
    
    #include <QObject>
    #include <QtSerialPort/QtSerialPort>
    #include "sharedclass.h"
    
    class SerialPort: public QSerialPort
    {
        Q_OBJECT
    public:
        SerialPort();    
    public slots:
        ...
    ....
        void sendData(const QByteArray &data);
        void readData();
        ....
    private slots:
        .....
    
    signals:
    
    private:
        QSerialPort *serial;
        SharedClass * sharedClass;
    };
    
    #endif // SERIALPORT_H
    
    

    SharedClass.cpp:

    #include "sharedclass.h"
    #include "serialport.h"
    
    SharedClass::SharedClass()
    {
       .....
    }
    void SharedClass::receiveData(QByteArray data)
    {
        qDebug()<<"receiveData";
        ...
    ...
    ...
    
        int lastCommand=getLastCommand();
        ......
    ...
    ...
    
        sendWaiteCommand();
    }
    void SharedClass::newFrame(){
        ....
        setLastCommand(..);
    }
    
    void SharedClass::setWaiteCommand(int data){
        waiteCommand=data;
    }
    void SharedClass::sendWaiteCommand(){
        ....
    ...
    serialPort->sendData(.....);
    }
    
    int SharedClass::getLastCommand()
    {
        return lastCommand;
    }
    void SharedClass::setLastCommand(int newLastCommand)
    {
        ....
    }
    
    

    SharedClass.h

    #ifndef SHAREDCLASS_H
    #define SHAREDCLASS_H
    
    #include <QObject>
    //#include "commands.h"
    #include "serialport.h"
    
    
    class SharedClass : public QObject
    {
        Q_OBJECT
        .......
    ......
    public:
        SharedClass();
        void receiveData(QByteArray data);
    
    public slots:
        ....
    private:
       
        SerialPort * serialPort;
    ...
    ...
    };
    
    #endif // SHAREDCLASS_H
    
    

    I use "class SharedClass : public SerialPort" but I have this error:

    base class undefined
    

  • Lifetime Qt Champion

    Hi
    in c++, its not allowed to let A include B and B include A also in the .h file
    That gives errors.
    One solution is to use a class forward

    class B; // forward and no include
    class A {
    B *theB;
    };

    #include "a"
    class B {
    A *theA;
    };

    That said, i have other note.
    you do

    SharedClass* _sharedClass = new SharedClass;
    sharedClass=_sharedClass;
    

    why not just

        sharedClass=new SharedClass;
    

    Also why does Shared need pointer to SerialPort ?
    and reverse?
    What does SharedClass do for SerialPort class ?



  • Thanks for your reply.

    @mrjj said in Qt - 2 classes use each other:

    Also why does Shared need pointer to SerialPort ?

    I wanna to process received data and also I want to have keep simple and basic functions in "serialport" class.

    @mrjj said in Qt - 2 classes use each other:

    What does SharedClass do for SerialPort class ?

    I want to send some Processed data to port.


  • Lifetime Qt Champion

    @neda cyclic dependencies smells like bad architecture, please overthink.

    for this specific case, signals&slots sound like a possible solution.



  • Thank you very much.

    @aha_1980 said in Qt - 2 classes use each other:

    cyclic dependencies smells like bad architecture, please overthink.

    Yes, that's right.

    @aha_1980 said in Qt - 2 classes use each other:

    for this specific case, signals&slots sound like a possible solution.

    I use this code:

    connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
    

    I do not know, I want read and process data in separate
    functions and from "processData" function send data to port.

    void SerialPort::readData()
    {
        QByteArray data = serial->read(1);
        qDebug()<<data;
        sharedClass->receiveData(data);    
    }
    void SharedClass::processData(QByteArray data)
    {
        if(data ....){
            serialPort->sendData(....);
        }
         else {
             serialPort->sendData(...);
          }
    }
    

    So it seems that my idea can not be implemented.


Log in to reply