Peculiar problem with QUndoCommand in a new class files.
-
Hello,
I have a project that was originally created in Qt 5.5 or older version. I have implemented the Undo/Redo Framework and that works like a charm.
However, i am facing a peculiar problem that i am unable to understand the reason.
At present I have around 15 files where different classes subclassing the QUndoCommand are created and working.
Say for example, the file abc1.h and abc1.c where 2 classes subclassing the QUndoCommand are defined.The problem happens when i create a new class header and c file and define a class subclassing the QUndoCommand.
Say for example, i create a file modifyprop.h and modifyprop.c and define the subclass of QUndoCommand. Giving here the typical class.
Header file modifypropcmd.h is as follows:#ifndef MODIFYPROPCMD_H #define MODIFYPROPCMD_H #include <QUndoCommand> #include "basegraphicentity.h" #include "mymainview.h" class modifypropcmd: public QUndoCommand { public: modifypropcmd(mymainview *thisview, baseblockentity *item,QUndoCommand *parent = 0); void undo(); void redo(); private: mymainview *myGraphview; baseblockentity *myitem; }; #endif // MODIFYPROPCMD_HThe CPP File "modifypropcmd.cpp is as follows:
#include "modifypropcmd.h" modifypropcmd::modifypropcmd(mymainview *thisview, baseblockentity *item,QUndoCommand *parent) :QUndoCommand(parent),myGraphview(thisview),myitem(item) { } void modifypropcmd::undo() { } void modifypropcmd::redo() { }This structure of a class is identical for over 100 different actions with undo commands.
This class when contained in a new class files does not work. It throws so many exceptions that i cannot understand. Here is a typical exception thrown.
C:\......\modifypropcmd.h:19: error: 'mymainview' does not name a type; did you mean 'mymainscene'? .......\Commands/modifypropcmd.h:19:5: error: 'mymainview' does not name a type; did you mean 'mymainscene'? mymainview *myGraphview; ^~~~~~~~~~ mymainsceneHowever, if i copy the entire class without any modification into another header and cpp file abc1.h and abc1.cpp which already has a similar class, the project compiles like a charm... without giving any errors.
Though I am able to continue working by creating these classes in existing files, I just dont know what could be the problem and i intend to solve it with help of experts here.
-
Hello,
I have a project that was originally created in Qt 5.5 or older version. I have implemented the Undo/Redo Framework and that works like a charm.
However, i am facing a peculiar problem that i am unable to understand the reason.
At present I have around 15 files where different classes subclassing the QUndoCommand are created and working.
Say for example, the file abc1.h and abc1.c where 2 classes subclassing the QUndoCommand are defined.The problem happens when i create a new class header and c file and define a class subclassing the QUndoCommand.
Say for example, i create a file modifyprop.h and modifyprop.c and define the subclass of QUndoCommand. Giving here the typical class.
Header file modifypropcmd.h is as follows:#ifndef MODIFYPROPCMD_H #define MODIFYPROPCMD_H #include <QUndoCommand> #include "basegraphicentity.h" #include "mymainview.h" class modifypropcmd: public QUndoCommand { public: modifypropcmd(mymainview *thisview, baseblockentity *item,QUndoCommand *parent = 0); void undo(); void redo(); private: mymainview *myGraphview; baseblockentity *myitem; }; #endif // MODIFYPROPCMD_HThe CPP File "modifypropcmd.cpp is as follows:
#include "modifypropcmd.h" modifypropcmd::modifypropcmd(mymainview *thisview, baseblockentity *item,QUndoCommand *parent) :QUndoCommand(parent),myGraphview(thisview),myitem(item) { } void modifypropcmd::undo() { } void modifypropcmd::redo() { }This structure of a class is identical for over 100 different actions with undo commands.
This class when contained in a new class files does not work. It throws so many exceptions that i cannot understand. Here is a typical exception thrown.
C:\......\modifypropcmd.h:19: error: 'mymainview' does not name a type; did you mean 'mymainscene'? .......\Commands/modifypropcmd.h:19:5: error: 'mymainview' does not name a type; did you mean 'mymainscene'? mymainview *myGraphview; ^~~~~~~~~~ mymainsceneHowever, if i copy the entire class without any modification into another header and cpp file abc1.h and abc1.cpp which already has a similar class, the project compiles like a charm... without giving any errors.
Though I am able to continue working by creating these classes in existing files, I just dont know what could be the problem and i intend to solve it with help of experts here.
@TheCrowKaka
What are you doing?! :confused:[BTW your problem won't be to do with
QUndoCommand.]class modifyblklaserpropcmd : public QUndoCommand { public: modifypropcmd(mymainview *thisview, baseblockentity *item,QUndoCommand *parent = 0);This declares a new class named
modifyblklaserpropcmdand then declares a constructor for classmodifypropcmdand no constructor formodifyblklaserpropcmd....And then in the
.cppyou have the same: constructor formodifypropcmd, rest of methods formodifyblklaserpropcmd.Can you start by sorting this out correctly, before we deal with
#ifndef MODIFYPROPCMD_Hwhich presumably should read#ifndef MODIFYBLKLASERPROPCMD_H, and why you haveclass modifyblklaserpropcmdin files namedmodifyprop...{cpp,h}and then there is amodifypropcmd.hfile....If this is all "deliberate" then I leave you to figure, because the architecture is too hard for me to get my head around.
If it's complaining
error: 'mymainview' does not name a typethen it has not met aclass mymainviewwhen it hits that line. If that is supposed to come from#include "mymainview.h"then my guess is that file has some complex#ifndef ...which in your scheme causesclass mymainviewnot to have been encountered.... -
@TheCrowKaka
What are you doing?! :confused:[BTW your problem won't be to do with
QUndoCommand.]class modifyblklaserpropcmd : public QUndoCommand { public: modifypropcmd(mymainview *thisview, baseblockentity *item,QUndoCommand *parent = 0);This declares a new class named
modifyblklaserpropcmdand then declares a constructor for classmodifypropcmdand no constructor formodifyblklaserpropcmd....And then in the
.cppyou have the same: constructor formodifypropcmd, rest of methods formodifyblklaserpropcmd.Can you start by sorting this out correctly, before we deal with
#ifndef MODIFYPROPCMD_Hwhich presumably should read#ifndef MODIFYBLKLASERPROPCMD_H, and why you haveclass modifyblklaserpropcmdin files namedmodifyprop...{cpp,h}and then there is amodifypropcmd.hfile....If this is all "deliberate" then I leave you to figure, because the architecture is too hard for me to get my head around.
If it's complaining
error: 'mymainview' does not name a typethen it has not met aclass mymainviewwhen it hits that line. If that is supposed to come from#include "mymainview.h"then my guess is that file has some complex#ifndef ...which in your scheme causesclass mymainviewnot to have been encountered....@JonB SORRY for the trouble..
Happened due to the copy pasting affair.
I have corrected the post. -
@JonB SORRY for the trouble..
Happened due to the copy pasting affair.
I have corrected the post.@TheCrowKaka said in Peculiar problem with QUndoCommand in a new class files.:
@JonB SORRY for the trouble..
Happened due to the copy pasting affair.Grrrrrrr :@ Do you realize how many times I have answered people's posts, which are said to be a "copy/paste" of their code, and then turn out not to be, and I have spent all the time to answer...?
So we go back to my last paragraph:
If it's complaining error: 'mymainview' does not name a type then it has not met a class mymainview when it hits that line. If that is supposed to come from #include "mymainview.h" then my guess is that file has some complex #ifndef ... which in your scheme causes class mymainview not to have been encountered....
#include "mymainview.h" class modifypropcmd: public QUndoCommand { private: mymainview *myGraphview;C:\......\modifypropcmd.h:19: error: 'mymainview' does not name a type; did you mean 'mymainscene'? .......\Commands/modifypropcmd.h:19:5: error: 'mymainview' does not name a type; did you mean 'mymainscene'? mymainview *myGraphview; ^~~~~~~~~~So
mymainviewis not declared when it gets tomodifypropcmd.h:19. So what exactly is in your"mymainview.h", if that is where it is supposed to come from ... ? -
@JonB said in Peculiar problem with QUndoCommand in a new class files.:
mymainview
simply forward declare the
mymainviewclass. -
@JonB said in Peculiar problem with QUndoCommand in a new class files.:
mymainview
simply forward declare the
mymainviewclass.@Christian-Ehrlicher
Since he has#include "mymainview.h"at the top of the file prior tomymainview *myGraphview;(as I pasted), don't you think that is supposed to bring in its declaration without any need/intention for forward declarations?? Which is why we need to see that file before deciding what the best "fix" is.... -
@Christian-Ehrlicher
Since he has#include "mymainview.h"at the top of the file prior tomymainview *myGraphview;(as I pasted), don't you think that is supposed to bring in its declaration without any need/intention for forward declarations?? Which is why we need to see that file before deciding what the best "fix" is....@JonB said in Peculiar problem with QUndoCommand in a new class files.:
don't you think that is supposed to bring in its declaration without any need/intention for forward declarations??
No because he includes
modifypropcmd.hinsidemymainview.hso there is a circular dependency -
@JonB said in Peculiar problem with QUndoCommand in a new class files.:
don't you think that is supposed to bring in its declaration without any need/intention for forward declarations??
No because he includes
modifypropcmd.hinsidemymainview.hso there is a circular dependency@Christian-Ehrlicher said in Peculiar problem with QUndoCommand in a new class files.:
No because he includes modifypropcmd.h inside mymainview.h so there is a circular dependency
If that is the case your answer makes sense. But how in the world do you know that, since he has never shown
mymainview.h?? Unless I am going mad and haven't spotted something.... [Or you are clairvoyant...] -
@Christian-Ehrlicher said in Peculiar problem with QUndoCommand in a new class files.:
No because he includes modifypropcmd.h inside mymainview.h so there is a circular dependency
If that is the case your answer makes sense. But how in the world do you know that, since he has never shown
mymainview.h?? Unless I am going mad and haven't spotted something.... [Or you are clairvoyant...]@JonB said in Peculiar problem with QUndoCommand in a new class files.:
Unless I am going mad and haven't spotted something....
I don't know it but it's a common error when you see such an error (you include a header, use the class but the compiler don't know about it).
There is no need to include a header file in another one when just the pointer to the class is used - use forward decls for such kind - it avoids circular dependency errors and speed up compilation. -
@JonB said in Peculiar problem with QUndoCommand in a new class files.:
Unless I am going mad and haven't spotted something....
I don't know it but it's a common error when you see such an error (you include a header, use the class but the compiler don't know about it).
There is no need to include a header file in another one when just the pointer to the class is used - use forward decls for such kind - it avoids circular dependency errors and speed up compilation.@Christian-Ehrlicher said in Peculiar problem with QUndoCommand in a new class files.:
I don't know
Ah ha! Better :) So you never intended "No because he includes", rather "If he includes"...
-
If @Christian-Ehrlicher has been correctly clairvoyant about your
mymainview.hhaving#include "modifypropcmd.h"in it then his is your answer. -
OTOH, if you need a mere mortal like myself --- no super-powers --- to help still, unlike @Christian-Ehrlicher I need to actually see what is in your
mymainview.hfile....
:)
-
-
@Christian-Ehrlicher, @JonB
Thanks for your answers.I was about to type the elaborate answer, but while doing that i realised the error.
As @Christian-Ehrlicher indicated,
No because he includes modifypropcmd.h inside mymainview.h so there is a circular dependencyThat was the exact problem.
I moved the modifypropcmd.h include from mymainview.h to mymainview.cpp and the solution compiled.Thanks both for helping me out.
-
@Christian-Ehrlicher, @JonB
Thanks for your answers.I was about to type the elaborate answer, but while doing that i realised the error.
As @Christian-Ehrlicher indicated,
No because he includes modifypropcmd.h inside mymainview.h so there is a circular dependencyThat was the exact problem.
I moved the modifypropcmd.h include from mymainview.h to mymainview.cpp and the solution compiled.Thanks both for helping me out.
@TheCrowKaka said in Peculiar problem with QUndoCommand in a new class files.:
As @Christian-Ehrlicher indicated,
No because he includes modifypropcmd.h inside mymainview.h so there is a circular dependency
That was the exact problem.I give up! If @Christian-Ehrlicher has enough telepathy to answer questions here without even seeing the code, what is the point in my even trying.... :( :) !!
-
@TheCrowKaka said in Peculiar problem with QUndoCommand in a new class files.:
As @Christian-Ehrlicher indicated,
No because he includes modifypropcmd.h inside mymainview.h so there is a circular dependency
That was the exact problem.I give up! If @Christian-Ehrlicher has enough telepathy to answer questions here without even seeing the code, what is the point in my even trying.... :( :) !!
@JonB said in Peculiar problem with QUndoCommand in a new class files.:
what is the point in my even trying.... :( :) !!
:D
This is just more than 20 years of experience - don't know how often I hit this problem by myself until I switched to forward decl. nearly everything what's possible :)