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. syntax error

syntax error

Scheduled Pinned Locked Moved Unsolved General and Desktop
qt6c++ windows
6 Posts 4 Posters 725 Views
  • 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.
  • A Offline
    A Offline
    a_coder
    wrote on 1 Mar 2024, 13:00 last edited by a_coder 3 Jan 2024, 13:12
    #1

    i need help with my code
    control.h

    #ifndef CONTROLER_H
    #define CONTROLER_H
    
    #include <qobject.h>
    #include <qtimer.h>
    #include <qobject.h>
    #include "main_.h"
    class Timer :public QObject {
        Q_OBJECT
    public:
        Timer(long long timeremain, QObject* parent = nullptr)
            :QObject(parent)
            ,counter(new QTimer)
        {
            timereaining = timeremain;
            connect(counter, &QTimer::timeout, this, sendupdate);
        }
    signals:
        void requestupdated(int remainsecond);
        void timeout();
    private slots:
        void sendupdate() {
            if (timereaining <= 0) {
                emit timeout();
            }
            else {
                timereaining--;
                emit requestupdated(timereaining);
            }
        }
    private:
        long long timereaining = 0;
        QTimer* counter;
    };
    
    class control :public QObject {
        Q_OBJECT
    public:
        control(long long time, QObject* parent = nullptr) 
            :QObject(parent)
        {
            main = new Main(timer);
            this->time = time;
        }
        ~control() {
            delete main;
        }
        void start() {
            main->show();
        }
    private:
        Timer* timer;
        Main* main;
        long long time = 0;
    };
    #endif
    

    main_.h

    #include <ui_mainwindow.h>
    #include <qmainwindow.h>
    #include "control.h"
    QT_BEGIN_NAMESPACE
    namespace Ui {
        class Main;
    }
    QT_END_NAMESPACE
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    class Main : public QMainWindow {
        Q_OBJECT
    public:
        void start();
        Main(Timer* time, QWidget* parent = nullptr);
        ~Main();
    signals:
        void change_gui();
    private:
        Ui::Main *ui;
        QLabel* label;
        Timer* time;
    private slots:
        void updatetime(long long time);
        void change_ui() {
            emit change_gui();
        }
    };
    #endif // !MAINWINDOW_H
    

    i think the main error is here

    1>error C2061: syntax error: identifier 'Timer'
    1> error C2143: syntax error: missing ';' before '*'
    1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>error C2238: unexpected token(s) preceding ';'
    

    but i can't see where the error from and how to fix that

    C J 2 Replies Last reply 1 Mar 2024, 13:28
    0
    • A a_coder
      1 Mar 2024, 13:00

      i need help with my code
      control.h

      #ifndef CONTROLER_H
      #define CONTROLER_H
      
      #include <qobject.h>
      #include <qtimer.h>
      #include <qobject.h>
      #include "main_.h"
      class Timer :public QObject {
          Q_OBJECT
      public:
          Timer(long long timeremain, QObject* parent = nullptr)
              :QObject(parent)
              ,counter(new QTimer)
          {
              timereaining = timeremain;
              connect(counter, &QTimer::timeout, this, sendupdate);
          }
      signals:
          void requestupdated(int remainsecond);
          void timeout();
      private slots:
          void sendupdate() {
              if (timereaining <= 0) {
                  emit timeout();
              }
              else {
                  timereaining--;
                  emit requestupdated(timereaining);
              }
          }
      private:
          long long timereaining = 0;
          QTimer* counter;
      };
      
      class control :public QObject {
          Q_OBJECT
      public:
          control(long long time, QObject* parent = nullptr) 
              :QObject(parent)
          {
              main = new Main(timer);
              this->time = time;
          }
          ~control() {
              delete main;
          }
          void start() {
              main->show();
          }
      private:
          Timer* timer;
          Main* main;
          long long time = 0;
      };
      #endif
      

      main_.h

      #include <ui_mainwindow.h>
      #include <qmainwindow.h>
      #include "control.h"
      QT_BEGIN_NAMESPACE
      namespace Ui {
          class Main;
      }
      QT_END_NAMESPACE
      #ifndef MAINWINDOW_H
      #define MAINWINDOW_H
      
      class Main : public QMainWindow {
          Q_OBJECT
      public:
          void start();
          Main(Timer* time, QWidget* parent = nullptr);
          ~Main();
      signals:
          void change_gui();
      private:
          Ui::Main *ui;
          QLabel* label;
          Timer* time;
      private slots:
          void updatetime(long long time);
          void change_ui() {
              emit change_gui();
          }
      };
      #endif // !MAINWINDOW_H
      

      i think the main error is here

      1>error C2061: syntax error: identifier 'Timer'
      1> error C2143: syntax error: missing ';' before '*'
      1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
      1>error C2238: unexpected token(s) preceding ';'
      

      but i can't see where the error from and how to fix that

      C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 1 Mar 2024, 13:28 last edited by
      #2

      You have an recursive include main_.h includes control.h and the other way round. This can not work - use forward declaration of classes.

      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
      3
      • P Offline
        P Offline
        Pl45m4
        wrote on 1 Mar 2024, 13:29 last edited by Pl45m4 3 Jan 2024, 13:32
        #3

        @a_coder

        The first thing I've noticed is that you have a circular dependency. Both headers include each other

        Edit:
        Any why you have two QObject in your header only class? I can imagine that this might also cause trouble?

        long long time = 0;

        long long time made me giggle :D


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

        ~E. W. Dijkstra

        1 Reply Last reply
        0
        • A a_coder
          1 Mar 2024, 13:00

          i need help with my code
          control.h

          #ifndef CONTROLER_H
          #define CONTROLER_H
          
          #include <qobject.h>
          #include <qtimer.h>
          #include <qobject.h>
          #include "main_.h"
          class Timer :public QObject {
              Q_OBJECT
          public:
              Timer(long long timeremain, QObject* parent = nullptr)
                  :QObject(parent)
                  ,counter(new QTimer)
              {
                  timereaining = timeremain;
                  connect(counter, &QTimer::timeout, this, sendupdate);
              }
          signals:
              void requestupdated(int remainsecond);
              void timeout();
          private slots:
              void sendupdate() {
                  if (timereaining <= 0) {
                      emit timeout();
                  }
                  else {
                      timereaining--;
                      emit requestupdated(timereaining);
                  }
              }
          private:
              long long timereaining = 0;
              QTimer* counter;
          };
          
          class control :public QObject {
              Q_OBJECT
          public:
              control(long long time, QObject* parent = nullptr) 
                  :QObject(parent)
              {
                  main = new Main(timer);
                  this->time = time;
              }
              ~control() {
                  delete main;
              }
              void start() {
                  main->show();
              }
          private:
              Timer* timer;
              Main* main;
              long long time = 0;
          };
          #endif
          

          main_.h

          #include <ui_mainwindow.h>
          #include <qmainwindow.h>
          #include "control.h"
          QT_BEGIN_NAMESPACE
          namespace Ui {
              class Main;
          }
          QT_END_NAMESPACE
          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H
          
          class Main : public QMainWindow {
              Q_OBJECT
          public:
              void start();
              Main(Timer* time, QWidget* parent = nullptr);
              ~Main();
          signals:
              void change_gui();
          private:
              Ui::Main *ui;
              QLabel* label;
              Timer* time;
          private slots:
              void updatetime(long long time);
              void change_ui() {
                  emit change_gui();
              }
          };
          #endif // !MAINWINDOW_H
          

          i think the main error is here

          1>error C2061: syntax error: identifier 'Timer'
          1> error C2143: syntax error: missing ';' before '*'
          1>error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
          1>error C2238: unexpected token(s) preceding ';'
          

          but i can't see where the error from and how to fix that

          J Offline
          J Offline
          JonB
          wrote on 1 Mar 2024, 14:50 last edited by JonB 3 Jan 2024, 14:51
          #4

          @a_coder
          As @Christian-Ehrlicher has said about the mutual recursion, and the need for forward declaration if you want to solve that.

          However, my initial reaction is: what is going on, what are you trying to achieve? It is "strange" to have some control object creating and showing your main window. Let alone the passing of the Timer object to it. I wonder what your architecture is and whether this is the best way to go about it. It is possible this is correct, but since you seem to be a beginner here my hunch is that this may not be the best approach.

          A 1 Reply Last reply 2 Mar 2024, 05:53
          0
          • J JonB
            1 Mar 2024, 14:50

            @a_coder
            As @Christian-Ehrlicher has said about the mutual recursion, and the need for forward declaration if you want to solve that.

            However, my initial reaction is: what is going on, what are you trying to achieve? It is "strange" to have some control object creating and showing your main window. Let alone the passing of the Timer object to it. I wonder what your architecture is and whether this is the best way to go about it. It is possible this is correct, but since you seem to be a beginner here my hunch is that this may not be the best approach.

            A Offline
            A Offline
            a_coder
            wrote on 2 Mar 2024, 05:53 last edited by
            #5

            @JonB my idea is make the control class to swich qmainwindow between qmainwidow with a class timer to synchronize all the timer in all the windows so i think that is the best way to go

            J 1 Reply Last reply 2 Mar 2024, 08:55
            0
            • A a_coder
              2 Mar 2024, 05:53

              @JonB my idea is make the control class to swich qmainwindow between qmainwidow with a class timer to synchronize all the timer in all the windows so i think that is the best way to go

              J Offline
              J Offline
              JonB
              wrote on 2 Mar 2024, 08:55 last edited by
              #6

              @a_coder
              I don't claim to understand what you are trying to achieve, but let's assume this is the way you want to go. Then I can see why the control class might need to know about the mainwindow class. But I cannot see why the latter would then need to know about the former, or what you are passing/sharing between them. If you got rid of that dependency direction you might not need your mutual references.

              1 Reply Last reply
              1

              5/6

              2 Mar 2024, 05:53

              • Login

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