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. error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp
Forum Updated to NodeBB v4.3 + New Features

error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp

Scheduled Pinned Locked Moved Solved General and Desktop
20 Posts 7 Posters 1.8k 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.
  • H Offline
    H Offline
    HoaiNam10TH
    wrote on 4 Dec 2023, 07:37 last edited by
    #1

    when I use:

    _io->socket()->emit("join conversation", data);
    

    there is an error, use the following method: error: expected unqualified-id before '(' token
    I have attached a photo of the error:
    socketerr.jpg

    I don't understand where I went wrong. please help me.

    J J 2 Replies Last reply 4 Dec 2023, 07:39
    0
    • H HoaiNam10TH
      4 Dec 2023, 07:37

      when I use:

      _io->socket()->emit("join conversation", data);
      

      there is an error, use the following method: error: expected unqualified-id before '(' token
      I have attached a photo of the error:
      socketerr.jpg

      I don't understand where I went wrong. please help me.

      J Offline
      J Offline
      jsulm
      Lifetime Qt Champion
      wrote on 4 Dec 2023, 07:39 last edited by jsulm 12 Apr 2023, 07:39
      #2

      @HoaiNam10TH Why are you trying to emit a signal on another object? Objects should emit their signals by themselfs.
      And please post code as text not pictures.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      H 1 Reply Last reply 4 Dec 2023, 12:08
      1
      • H HoaiNam10TH
        4 Dec 2023, 07:37

        when I use:

        _io->socket()->emit("join conversation", data);
        

        there is an error, use the following method: error: expected unqualified-id before '(' token
        I have attached a photo of the error:
        socketerr.jpg

        I don't understand where I went wrong. please help me.

        J Offline
        J Offline
        JonB
        wrote on 4 Dec 2023, 07:50 last edited by JonB 12 Apr 2023, 08:01
        #3

        @HoaiNam10TH
        Further to @jsulm, and his correct statement that objects should (generally) only do their own signal emission.
        In Qt you can only go:

        emit signal();
        

        In fact, emit is #defined to be empty/no expansion --- it is only "syntactic sugar" to help you identify where signals are emitted. There is no function named emit() anywhere, you will never be able to go:

        something->emit(...);
        

        If you really want to do this the correct syntax will be:

        emit object->signal(...);
        
        H 1 Reply Last reply 4 Dec 2023, 08:00
        2
        • J JonB
          4 Dec 2023, 07:50

          @HoaiNam10TH
          Further to @jsulm, and his correct statement that objects should (generally) only do their own signal emission.
          In Qt you can only go:

          emit signal();
          

          In fact, emit is #defined to be empty/no expansion --- it is only "syntactic sugar" to help you identify where signals are emitted. There is no function named emit() anywhere, you will never be able to go:

          something->emit(...);
          

          If you really want to do this the correct syntax will be:

          emit object->signal(...);
          
          H Offline
          H Offline
          HoaiNam10TH
          wrote on 4 Dec 2023, 08:00 last edited by
          #4

          @JonB Thank you, I understand

          1 Reply Last reply
          0
          • J jsulm
            4 Dec 2023, 07:39

            @HoaiNam10TH Why are you trying to emit a signal on another object? Objects should emit their signals by themselfs.
            And please post code as text not pictures.

            H Offline
            H Offline
            HoaiNam10TH
            wrote on 4 Dec 2023, 12:08 last edited by HoaiNam10TH 12 Apr 2023, 12:10
            #5

            @jsulm @JonB
            I still can't do it, here is my code, please help me.
            Widget.h

            #include <sio_client.h>   
            class Widget : public QWidget
            {
                Q_OBJECT
            public:
                Widget(QWidget *parent = nullptr);
                ~Widget();    
            signals:
                void joinConversation(std::string userId);
            private slots:
            private:
                std::unique_ptr<sio::client> _io;
            }; 
            

            Widget.cpp

            connect(this,&Widget::joinConversation,[=](std::string userId){
            	_io->socket()->emit("join conversation", userId); // **error**
            });
            
            void Widget::on_listWidgetContact_itemClicked(QListWidgetItem *item)
            {    
                QJsonObject userJson = item->data(Qt::UserRole).toJsonObject();    
                QByteArray bytes = userJson["_id"].toString().toUtf8();
                std::string userId(bytes.data(),bytes.length());
                emit joinConversation(userId);
            }
            
            J P 2 Replies Last reply 4 Dec 2023, 12:18
            0
            • H HoaiNam10TH
              4 Dec 2023, 12:08

              @jsulm @JonB
              I still can't do it, here is my code, please help me.
              Widget.h

              #include <sio_client.h>   
              class Widget : public QWidget
              {
                  Q_OBJECT
              public:
                  Widget(QWidget *parent = nullptr);
                  ~Widget();    
              signals:
                  void joinConversation(std::string userId);
              private slots:
              private:
                  std::unique_ptr<sio::client> _io;
              }; 
              

              Widget.cpp

              connect(this,&Widget::joinConversation,[=](std::string userId){
              	_io->socket()->emit("join conversation", userId); // **error**
              });
              
              void Widget::on_listWidgetContact_itemClicked(QListWidgetItem *item)
              {    
                  QJsonObject userJson = item->data(Qt::UserRole).toJsonObject();    
                  QByteArray bytes = userJson["_id"].toString().toUtf8();
                  std::string userId(bytes.data(),bytes.length());
                  emit joinConversation(userId);
              }
              
              J Offline
              J Offline
              J.Hilk
              Moderators
              wrote on 4 Dec 2023, 12:18 last edited by
              #6

              @HoaiNam10TH said in error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp:

              connect(this,&Widget::joinConversation,[=](std::string userId){
              _io->socket()->emit("join conversation", userId); // error
              });

              well, yes.
              Like @JonB previously stated, the "emit" is in the wrong place, you're also missing the actual signal/function name in your call


              Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


              Q: What's that?
              A: It's blue light.
              Q: What does it do?
              A: It turns blue.

              H 1 Reply Last reply 4 Dec 2023, 12:21
              1
              • J J.Hilk
                4 Dec 2023, 12:18

                @HoaiNam10TH said in error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp:

                connect(this,&Widget::joinConversation,[=](std::string userId){
                _io->socket()->emit("join conversation", userId); // error
                });

                well, yes.
                Like @JonB previously stated, the "emit" is in the wrong place, you're also missing the actual signal/function name in your call

                H Offline
                H Offline
                HoaiNam10TH
                wrote on 4 Dec 2023, 12:21 last edited by
                #7

                @J-Hilk Can you teach me how to fix this error?

                J 1 Reply Last reply 4 Dec 2023, 12:24
                0
                • H HoaiNam10TH
                  4 Dec 2023, 12:21

                  @J-Hilk Can you teach me how to fix this error?

                  J Offline
                  J Offline
                  J.Hilk
                  Moderators
                  wrote on 4 Dec 2023, 12:24 last edited by
                  #8

                  @HoaiNam10TH I don't know what your signal that you try to call is named.

                  replace emit:
                  _io->socket()->emit("join conversation", userId);

                  with the signal name:
                  void joinConversation(std::string userId);


                  Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                  Q: What's that?
                  A: It's blue light.
                  Q: What does it do?
                  A: It turns blue.

                  1 Reply Last reply
                  1
                  • H HoaiNam10TH
                    4 Dec 2023, 12:08

                    @jsulm @JonB
                    I still can't do it, here is my code, please help me.
                    Widget.h

                    #include <sio_client.h>   
                    class Widget : public QWidget
                    {
                        Q_OBJECT
                    public:
                        Widget(QWidget *parent = nullptr);
                        ~Widget();    
                    signals:
                        void joinConversation(std::string userId);
                    private slots:
                    private:
                        std::unique_ptr<sio::client> _io;
                    }; 
                    

                    Widget.cpp

                    connect(this,&Widget::joinConversation,[=](std::string userId){
                    	_io->socket()->emit("join conversation", userId); // **error**
                    });
                    
                    void Widget::on_listWidgetContact_itemClicked(QListWidgetItem *item)
                    {    
                        QJsonObject userJson = item->data(Qt::UserRole).toJsonObject();    
                        QByteArray bytes = userJson["_id"].toString().toUtf8();
                        std::string userId(bytes.data(),bytes.length());
                        emit joinConversation(userId);
                    }
                    
                    P Online
                    P Online
                    Pl45m4
                    wrote on 4 Dec 2023, 12:50 last edited by Pl45m4 12 Apr 2023, 12:52
                    #9

                    @HoaiNam10TH said in error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp:

                    connect(this,&Widget::joinConversation,[=](std::string userId){
                    _io->socket()->emit("join conversation", userId); // error
                    });

                    So you are just forwarding your signal from Widget to _io->socket?
                    If socket has a public signal called joinConversation you can simply call it, which is nothing else what emit does, since emit itself does bascially nothing :)

                    _io->socket()->joinConversation(userId);
                    

                    But when used like this, it's hard to see that joinConversation is a signal and not the fuunction that's executed after the call. Therefore the emit keyword. Besides all this, "calling" signals from other places is not the best practise and somehow bad design, like everybody else above already said


                    If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                    ~E. W. Dijkstra

                    H 1 Reply Last reply 4 Dec 2023, 15:45
                    0
                    • P Pl45m4
                      4 Dec 2023, 12:50

                      @HoaiNam10TH said in error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp:

                      connect(this,&Widget::joinConversation,[=](std::string userId){
                      _io->socket()->emit("join conversation", userId); // error
                      });

                      So you are just forwarding your signal from Widget to _io->socket?
                      If socket has a public signal called joinConversation you can simply call it, which is nothing else what emit does, since emit itself does bascially nothing :)

                      _io->socket()->joinConversation(userId);
                      

                      But when used like this, it's hard to see that joinConversation is a signal and not the fuunction that's executed after the call. Therefore the emit keyword. Besides all this, "calling" signals from other places is not the best practise and somehow bad design, like everybody else above already said

                      H Offline
                      H Offline
                      HoaiNam10TH
                      wrote on 4 Dec 2023, 15:45 last edited by
                      #10

                      @Pl45m4 this a a example with their code. It's run good. But my project is failed.

                      void MainWindow::SendBtnClicked()
                      {
                          QLineEdit* messageEdit = this->findChild<QLineEdit*>("messageEdit");
                          QString text = messageEdit->text();
                          if(text.length()>0)
                          {
                              QByteArray bytes = text.toUtf8();
                              std::string msg(bytes.data(),bytes.length());
                              _io->socket()->emit("new message",msg);
                              text.append(" : You");
                              QListWidgetItem *item = new QListWidgetItem(text);
                              item->setTextAlignment(Qt::AlignRight);
                              Q_EMIT RequestAddListItem(item);
                              messageEdit->clear();
                          }
                      }
                      

                      _io->socket()->emit("new message",msg);

                      P 1 Reply Last reply 4 Dec 2023, 16:01
                      0
                      • H HoaiNam10TH
                        4 Dec 2023, 15:45

                        @Pl45m4 this a a example with their code. It's run good. But my project is failed.

                        void MainWindow::SendBtnClicked()
                        {
                            QLineEdit* messageEdit = this->findChild<QLineEdit*>("messageEdit");
                            QString text = messageEdit->text();
                            if(text.length()>0)
                            {
                                QByteArray bytes = text.toUtf8();
                                std::string msg(bytes.data(),bytes.length());
                                _io->socket()->emit("new message",msg);
                                text.append(" : You");
                                QListWidgetItem *item = new QListWidgetItem(text);
                                item->setTextAlignment(Qt::AlignRight);
                                Q_EMIT RequestAddListItem(item);
                                messageEdit->clear();
                            }
                        }
                        

                        _io->socket()->emit("new message",msg);

                        P Online
                        P Online
                        Pl45m4
                        wrote on 4 Dec 2023, 16:01 last edited by Pl45m4 12 Apr 2023, 16:05
                        #11

                        @HoaiNam10TH said in error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp:

                        this a a example with their code.

                        Their code? Whose code is this?

                        _io->socket()->emit("new message",msg);

                        There could be a function called emit() which has nothing to do with Qt, but sets up a connection or something else...
                        Using the emit (from QObject) in a way like this should never work.

                        If you really want to emit the signal in _io->socket, then use what @J-Hilk suggested and what I wrote here

                        @Pl45m4 said in error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp:

                        _io->socket()->joinConversation(userId);

                        If possible show how _io->socket()->emit("new message",msg); is defined...


                        If debugging is the process of removing software bugs, then programming must be the process of putting them in.

                        ~E. W. Dijkstra

                        H 1 Reply Last reply 4 Dec 2023, 16:20
                        0
                        • P Pl45m4
                          4 Dec 2023, 16:01

                          @HoaiNam10TH said in error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp:

                          this a a example with their code.

                          Their code? Whose code is this?

                          _io->socket()->emit("new message",msg);

                          There could be a function called emit() which has nothing to do with Qt, but sets up a connection or something else...
                          Using the emit (from QObject) in a way like this should never work.

                          If you really want to emit the signal in _io->socket, then use what @J-Hilk suggested and what I wrote here

                          @Pl45m4 said in error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp:

                          _io->socket()->joinConversation(userId);

                          If possible show how _io->socket()->emit("new message",msg); is defined...

                          H Offline
                          H Offline
                          HoaiNam10TH
                          wrote on 4 Dec 2023, 16:20 last edited by
                          #12

                          @Pl45m4 Follow this github page, it has an example of socket.io with cpp (examples folder). Their project runs fine on my machine:
                          _io->socket()->emit("new message",msg);
                          emit("new message",msg) it is a function (owned by socket.io-client-cpp) to invoke method on nodejs server. It is not emit of Qt.

                          J SGaistS 2 Replies Last reply 4 Dec 2023, 17:56
                          0
                          • H HoaiNam10TH
                            4 Dec 2023, 16:20

                            @Pl45m4 Follow this github page, it has an example of socket.io with cpp (examples folder). Their project runs fine on my machine:
                            _io->socket()->emit("new message",msg);
                            emit("new message",msg) it is a function (owned by socket.io-client-cpp) to invoke method on nodejs server. It is not emit of Qt.

                            J Offline
                            J Offline
                            JonB
                            wrote on 4 Dec 2023, 17:56 last edited by JonB 12 Apr 2023, 17:58
                            #13

                            @HoaiNam10TH said in error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp:

                            emit("new message",msg) it is a function (owned by socket.io-client-cpp) to invoke method on nodejs server. It is not emit of Qt.

                            Then this is the problem/cause of the error message. Like I said earlier, Qt header files go

                            #define emit
                            

                            Just like that. That will make your _io->socket()->emit("new message",msg); become _io->socket()->("new message",msg); and hence error: expected unqualified-id before ‘(’ token.

                            Do not include Qt header files before the _io->socket()->emit("new message",msg); line. If you cannot do that you may be able to go #undef emit around that _io->socket()->emit(...) line.

                            Christian EhrlicherC 1 Reply Last reply 4 Dec 2023, 18:42
                            1
                            • J JonB
                              4 Dec 2023, 17:56

                              @HoaiNam10TH said in error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp:

                              emit("new message",msg) it is a function (owned by socket.io-client-cpp) to invoke method on nodejs server. It is not emit of Qt.

                              Then this is the problem/cause of the error message. Like I said earlier, Qt header files go

                              #define emit
                              

                              Just like that. That will make your _io->socket()->emit("new message",msg); become _io->socket()->("new message",msg); and hence error: expected unqualified-id before ‘(’ token.

                              Do not include Qt header files before the _io->socket()->emit("new message",msg); line. If you cannot do that you may be able to go #undef emit around that _io->socket()->emit(...) line.

                              Christian EhrlicherC Offline
                              Christian EhrlicherC Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 4 Dec 2023, 18:42 last edited by
                              #14

                              @JonB Or use QT_NO_KEYWORDS :)

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              J 1 Reply Last reply 4 Dec 2023, 19:19
                              2
                              • Christian EhrlicherC Christian Ehrlicher
                                4 Dec 2023, 18:42

                                @JonB Or use QT_NO_KEYWORDS :)

                                J Offline
                                J Offline
                                JonB
                                wrote on 4 Dec 2023, 19:19 last edited by
                                #15

                                @Christian-Ehrlicher
                                Indeed, but if your own code being compiled is using emit before Qt signals you still have to deal with that. One way or the other it needs sorting.

                                Christian EhrlicherC 1 Reply Last reply 4 Dec 2023, 19:25
                                0
                                • J JonB
                                  4 Dec 2023, 19:19

                                  @Christian-Ehrlicher
                                  Indeed, but if your own code being compiled is using emit before Qt signals you still have to deal with that. One way or the other it needs sorting.

                                  Christian EhrlicherC Offline
                                  Christian EhrlicherC Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on 4 Dec 2023, 19:25 last edited by
                                  #16

                                  @JonB said in error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp:

                                  but if your own code being compiled is using emit before Qt signals you still have to deal with that

                                  Yes, either remove it or use Q_EMIT instead.

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  J 1 Reply Last reply 4 Dec 2023, 19:27
                                  1
                                  • Christian EhrlicherC Christian Ehrlicher
                                    4 Dec 2023, 19:25

                                    @JonB said in error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp:

                                    but if your own code being compiled is using emit before Qt signals you still have to deal with that

                                    Yes, either remove it or use Q_EMIT instead.

                                    J Offline
                                    J Offline
                                    JonB
                                    wrote on 4 Dec 2023, 19:27 last edited by JonB 12 Apr 2023, 19:28
                                    #17

                                    @Christian-Ehrlicher Ah. putting Q_EMIT instead of emit sounds like I good idea. Does Qt source do this?

                                    Christian EhrlicherC 1 Reply Last reply 4 Dec 2023, 19:34
                                    0
                                    • J JonB
                                      4 Dec 2023, 19:27

                                      @Christian-Ehrlicher Ah. putting Q_EMIT instead of emit sounds like I good idea. Does Qt source do this?

                                      Christian EhrlicherC Offline
                                      Christian EhrlicherC Offline
                                      Christian Ehrlicher
                                      Lifetime Qt Champion
                                      wrote on 4 Dec 2023, 19:34 last edited by
                                      #18

                                      @JonB said in error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp:

                                      Does Qt source do this?

                                      Qt source code does not use 'signals' or 'slots' or 'emit' in public headers - only the Q_foo keywords.

                                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                      Visit the Qt Academy at https://academy.qt.io/catalog

                                      1 Reply Last reply
                                      2
                                      • H HoaiNam10TH
                                        4 Dec 2023, 16:20

                                        @Pl45m4 Follow this github page, it has an example of socket.io with cpp (examples folder). Their project runs fine on my machine:
                                        _io->socket()->emit("new message",msg);
                                        emit("new message",msg) it is a function (owned by socket.io-client-cpp) to invoke method on nodejs server. It is not emit of Qt.

                                        SGaistS Offline
                                        SGaistS Offline
                                        SGaist
                                        Lifetime Qt Champion
                                        wrote on 4 Dec 2023, 19:46 last edited by
                                        #19

                                        @HoaiNam10TH said in error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp:

                                        @Pl45m4 Follow this github page, it has an example of socket.io with cpp (examples folder). Their project runs fine on my machine:
                                        _io->socket()->emit("new message",msg);
                                        emit("new message",msg) it is a function (owned by socket.io-client-cpp) to invoke method on nodejs server. It is not emit of Qt.

                                        Hi,

                                        The Qt example in the repository you link contains a README that explains that you need the no_keyword configuration option and why.

                                        Interested in AI ? www.idiap.ch
                                        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                                        H 1 Reply Last reply 5 Dec 2023, 00:54
                                        3
                                        • SGaistS SGaist
                                          4 Dec 2023, 19:46

                                          @HoaiNam10TH said in error: expected unqualified-id before ‘(’ token when using emit event of socket.io-client-cpp:

                                          @Pl45m4 Follow this github page, it has an example of socket.io with cpp (examples folder). Their project runs fine on my machine:
                                          _io->socket()->emit("new message",msg);
                                          emit("new message",msg) it is a function (owned by socket.io-client-cpp) to invoke method on nodejs server. It is not emit of Qt.

                                          Hi,

                                          The Qt example in the repository you link contains a README that explains that you need the no_keyword configuration option and why.

                                          H Offline
                                          H Offline
                                          HoaiNam10TH
                                          wrote on 5 Dec 2023, 00:54 last edited by
                                          #20

                                          @SGaist
                                          That's right. Thanks everyone

                                          1 Reply Last reply
                                          0
                                          • SGaistS SGaist has marked this topic as solved on 5 Dec 2023, 22:24

                                          6/20

                                          4 Dec 2023, 12:18

                                          14 unread
                                          • Login

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