Include header issue
-
Hi! I have two windows, main window and settings window (second).
For example:
On main window I have a button that will open settings window. On settings window I get some data from registry and need to set this data to main window. The problem is that I can't use connect do to this because I get error:
C2143: syntax error: missing ';' before '*'.This error tells that I use circle includes.
In main window header:
#include "appsettings.h"In Settings window header:
#include "mainwindow.hWithout includes I can't create the objects from the classes. How to fix this issue? Thanks.
-
Hi @Cobra91151
Can you share your code?
-
-
Most probably a forward declaration can be used in (at least) one of your header file. Then in the implementation file you can have your headers.
AFAIK,
C2143: syntax error: missing ';' before '*'doesn't tell you anything about circular dependencies.This error only appears when include headers, so I think the problem is with circle includes. What do you mean by forward declaration? Can you show an example?
-
This is basic C / C++ usage, see for instance forward declaration on wiklipedia or on stackoverflow.
-
Hi @Cobra91151
Can you share your code?
I can't share the code but I can create an example and upload it here.
-
Test project is available here - Mega. Thanks.
-
This is basic C / C++ usage, see for instance forward declaration on wiklipedia or on stackoverflow.
I know about forward declaration but it's not working in my case.
-
Hi Cobra91151,
if you forward declarate
TestWindowinappsettings.hlike @JohanSolo suggested#ifndef APPSETTINGS_H #define APPSETTINGS_H #include <QWidget> //********************* class TestWindow; namespace Ui { class AppSettings; } class AppSettings : public QWidget { Q_OBJECT public: explicit AppSettings(QWidget *parent = 0); ~AppSettings(); private: Ui::AppSettings *ui; TestWindow *mainWindow; }; #endif // APPSETTINGS_Hand include
testwindow.hin apsettings.cpp your project can be compiled#include "appsettings.h" #include "ui_appsettings.h" #include "testwindow.h" AppSettings::AppSettings(QWidget *parent) : QWidget(parent), ui(new Ui::AppSettings) { ui->setupUi(this); this->setWindowTitle("Settings"); this->setWindowFlags(Qt::Dialog | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint); mainWindow = new TestWindow(); } AppSettings::~AppSettings() { delete ui; }But a short look at your code showed, that your are creating a
AppSettingsobject inTestWindowconstructor.
InAppSettingsconstrutor you create aTestWindowobject, where you create aAppSettingsobject, where you
create aTestWindowobject, where you create aAppSettingsobject, ...I doubt this was your intention.
-
Hi Cobra91151,
if you forward declarate
TestWindowinappsettings.hlike @JohanSolo suggested#ifndef APPSETTINGS_H #define APPSETTINGS_H #include <QWidget> //********************* class TestWindow; namespace Ui { class AppSettings; } class AppSettings : public QWidget { Q_OBJECT public: explicit AppSettings(QWidget *parent = 0); ~AppSettings(); private: Ui::AppSettings *ui; TestWindow *mainWindow; }; #endif // APPSETTINGS_Hand include
testwindow.hin apsettings.cpp your project can be compiled#include "appsettings.h" #include "ui_appsettings.h" #include "testwindow.h" AppSettings::AppSettings(QWidget *parent) : QWidget(parent), ui(new Ui::AppSettings) { ui->setupUi(this); this->setWindowTitle("Settings"); this->setWindowFlags(Qt::Dialog | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint); mainWindow = new TestWindow(); } AppSettings::~AppSettings() { delete ui; }But a short look at your code showed, that your are creating a
AppSettingsobject inTestWindowconstructor.
InAppSettingsconstrutor you create aTestWindowobject, where you create aAppSettingsobject, where you
create aTestWindowobject, where you create aAppSettingsobject, ...I doubt this was your intention.
Yes, it compiles but not working. I just want for example, to set the main window title from settings window (or other communications between two windows). So how to do it?
-
Hi @Cobra91151
In your case take one more class(mainClass) that contains both headerfile and all connection between these two window should happens throgh this class only.
whenever window1 sends any signal catch it in your mainClass and connect that to window2 and vice versa. for this, your both window objects shoul create in mainClass.
this will give solution for your question -
If it solved then make it solved.
-
If it solved then make it solved.
I have added main class but the issue is still present.
Test project is available here - Mega.
-
still you made mistake,
you have created communicationWorker object in AppSetting and in communicationWorker you created testWindow object and then in testwindow you created appsetting object, so it again became cycle.
what i suggest you is,
in communicationWorker create both appsetting and testwindow objects,in communicationWorker made connections between appsetting and testwindow
whenever any signal emited in any of these two class catch those on communicationworker class and pass to required class.
what you made in above example that is wrong.
-
still you made mistake,
you have created communicationWorker object in AppSetting and in communicationWorker you created testWindow object and then in testwindow you created appsetting object, so it again became cycle.
what i suggest you is,
in communicationWorker create both appsetting and testwindow objects,in communicationWorker made connections between appsetting and testwindow
whenever any signal emited in any of these two class catch those on communicationworker class and pass to required class.
what you made in above example that is wrong.
OK. Then in what file I should include
communicationWorker.h? Thanks. -
I don't think you have clear the difference between a class and an instance of the class.
Personally I think that guerilla programming is evil so: http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf
-
I don't think you have clear the difference between a class and an instance of the class.
Personally I think that guerilla programming is evil so: http://www.bogotobogo.com/cplusplus/files/c-gui-programming-with-qt-4-2ndedition.pdf
class AppSettingsis a class
AppSettings *testSettings = new AppSettings()- is the instance of the class (heap);
AppSettings testSettings;- is the instance of the class (stack).But to get the instance of a class you should include the appropriate header file.
So I'm not right? -
still you made mistake,
you have created communicationWorker object in AppSetting and in communicationWorker you created testWindow object and then in testwindow you created appsetting object, so it again became cycle.
what i suggest you is,
in communicationWorker create both appsetting and testwindow objects,in communicationWorker made connections between appsetting and testwindow
whenever any signal emited in any of these two class catch those on communicationworker class and pass to required class.
what you made in above example that is wrong.
I have changed code to your suggestion but it's not emitting a signal.
Test project - Mega.
Can you show an example? Thanks.
-
class AppSettingsis a class
AppSettings *testSettings = new AppSettings()- is the instance of the class (heap);
AppSettings testSettings;- is the instance of the class (stack).But to get the instance of a class you should include the appropriate header file.
So I'm not right?@Cobra91151
you need to include the header file only when you instantiate an object.in your header file, you only declare pointers, so forward declaration is enough. you only need to include header file in cpp file
and in your code, your main function just have a
TestWindow, there is neitherCommunicationWorkernorAppSettings, so the connection betweenTestWindowandAppSettingsdo not exist.I think @Venkatesh-V's suggestion is creating a
CommunicationWorkeras the top object, so you only need to createCommunicationWorkerin the main function.but you can just declare
AppSettingsasTestWindow's member, and connect them in the constructor ofTestWindow -
@Cobra91151
you need to include the header file only when you instantiate an object.in your header file, you only declare pointers, so forward declaration is enough. you only need to include header file in cpp file
and in your code, your main function just have a
TestWindow, there is neitherCommunicationWorkernorAppSettings, so the connection betweenTestWindowandAppSettingsdo not exist.I think @Venkatesh-V's suggestion is creating a
CommunicationWorkeras the top object, so you only need to createCommunicationWorkerin the main function.but you can just declare
AppSettingsasTestWindow's member, and connect them in the constructor ofTestWindowI changed code to yours suggestions but windows are not communicate.
My codes and test project are available - Mega.
Can someone post an example or fix my test project? Thanks in advance.