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

Widget Server Client problem



  • Hello everybody, I have problem with the Server code of my widget.

    It says : " expected class-name before '{' token "
    And I searched over the internet but didnt find my solution

    Here is the code :

    #ifndef HELLOWORLDSERVER_H
    #define HELLOWORLDSERVER_H
    
    #include <QTcpServer>
    
    #include "MainWindow.h"
    class MainWindow;
    
    class HelloWorldServer : public QTcpServer
    {
    public:
    
        Q_OBJECT
    
            public:
                HelloWorldServer(MainWindow* pHelloServer,QObject *parent=0);
            MainWindow* m_pHelloWindow;
    
            private slots:
                void readyRead();
                void disconnected();
    
            protected:
                void incomingConnection(int socketfd);
    
            private:
                QSet<QTcpSocket*> clients;
    
    
    };
    
    #endif // HELLOWORLDSERVER_H
    

    I hope you will help me, Thank you


  • Lifetime Qt Champion

    Hi,

    What does MainWindow.h look like ?

    The Q_OBJECT macro should be in the private part of your class.

    Out of curiosity, why does your QTcpServer have to know about your MainWindow ? That smells like tight coupling which is a bad idea.



  • @SGaist Thank you for your answer, I actually took a part of code from the internet.

    Here is the the mainwindow.h :

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    #include "HelloWorldServer.h"
    
    namespace Ui {
    class MainWindow;
    }
    class HelloWorldServer;
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
        void addMessage(QString Msg);
        HelloWorldServer* m_pBoxServer;
    
    private slots:
        void on_pushButtonStart_clicked();
    
    private:
        Ui::MainWindow *ui;
    };
    
    #endif // MAINWINDOW_H
    
    

    I tried putting the Q_OBJECT into the private one and it gave me same errors with this one more : QTcpServer was not declared in this scope.



  • @lolilol78 Basically the widget for the client works fine (entering the ip adresse, the port, and connect push button.)

    but the server is stuck


  • Lifetime Qt Champion

    Did you add QT += network to your .pro file ?

    Your HelloWorldServer shouldn't know anything about MainWindow. You should have slots there that you will connect to the MainWindow appropriate signals.



  • @SGaist I added it yes .

    What do you mean by shouldnt know anything about?


  • Lifetime Qt Champion

    Then can you show the complete error log ?

    When you have XXX was not declared in this scope, it usually means that you didn't include the header which provides that class.

    I mean that your HelloWorldServer shouldn't need to know where from its data are coming. Just give it an interface that allows to set whatever parameter it needs.



  • @SGaist ok i see. here is the full error log :

    0_1533849878699_10b4c912-3f25-4df3-85b5-d89e75891d80-image.png


  • Lifetime Qt Champion

    You have circular dependencies.



  • @SGaist dont know about that, i will check the internet and come back to you. thank you for your help SGaist



  • @SGaist so it means that i have functions related to other .cpp inside my whole program?

    (sorry it is all new to me)


  • Lifetime Qt Champion

    No, it means you are including MainWindow.h in HelloWorldServer.h and then HelloWorldServer.h in MainWindow.h


Log in to reply