Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt - 2 classes use each other
Forum Updated to NodeBB v4.3 + New Features

Qt - 2 classes use each other

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 843 Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    neda
    wrote on last edited by neda
    #1

    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
    
    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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 ?

      N 1 Reply Last reply
      3
      • mrjjM mrjj

        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 ?

        N Offline
        N Offline
        neda
        wrote on last edited by
        #3

        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.

        aha_1980A 1 Reply Last reply
        0
        • N neda

          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.

          aha_1980A Offline
          aha_1980A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

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

          Qt has to stay free or it will die.

          N 1 Reply Last reply
          4
          • aha_1980A aha_1980

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

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

            N Offline
            N Offline
            neda
            wrote on last edited by neda
            #5

            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.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved