syntax error
-
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
-
You have an recursive include
main_.h
includescontrol.h
and the other way round. This can not work - use forward declaration of classes. -
The first thing I've noticed is that you have a circular dependency. Both headers include each other
Edit:
Any why you have twoQObject
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 -
@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 theTimer
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_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 thecontrol
class might need to know about themainwindow
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.