Circular include dependency issue with QOpenGLWindow ( is this a bug? )



  • I'm developing some OpenGL applications and I came through this little annoying issue with QOpenGLWindow. Apparently, the issue is the circular include dependency if I have a class whose name is Rectangle. After some digging and asking in another forum, it seems the name Rectangle is defined somewhere in Windows. The following minimal working example produces the issue.

    1- Create simple gui application (i.e. QMainWindow).
    2- Create class and name it Rectangle.
    3- Add #include <rectangle.h> and #include <QOpenGLWindow> inside mainwindow.h.
    4- Add Rectangle *box; to class MainWindow.
    5- Add opengl to .pro file

    You will get this annoying unclear error

    Rectangle *box; // <-- C2143: syntax error: missing ';' before '*'
    

    In the forum that I've asked the question, one suggests that the Rectangle function is defined in win32 API. Is this a bug or merely a rare coincidence and lucky I was aware of this error but I couldn't image that this function is defined somewhere? How can I avoid this subtle issues in the future?

    The compiler is MSVC2015 32bit.


  • Moderators

    @CroCo I cannot reproduce this issue on Windows 7 with MinGW as compiler.
    You should not include your own header files with <> do it like this:

    #include "rectangle.h"
    

    But with MinGW both variants work fine.

    Can you post the whole compiler output?



  • @jsulm

    The compiler is MSVC2015 32bit. I've tried #include "rectangle.h" with no luck. These are the errors I'm getting

    0_1501054844106_Capture.PNG


  • Moderators

    @CroCo Can you post the output from "Compile Output"?
    If there is really a name conflict you could put your own Rectangle class in a namespace.





  • @CroCo

    As @jsulm points out, the Rectangle is already defined in wingdi.h.
    Please use the namespace simply. like this:

    #include "rectangle.h"
    
    namespace MyRec {
    class Rectangle;
    }
    
    class MainWindow : public QMainWindow
    {
    [...]
    MyRec::Rectangle *box;
    [...]
    }
    


  • Yes, defining a namespace around your Rectangle is a good solution.

    This is a problem from the 80's before namespaces were invented, there are many example of Microsoft's headers polluting the global namespace (on the Mac everything is prefixed with 'NS..." from the NextStep days but Microsoft never uses an MS prefix :-(

    Anyway, to unpollute the global namespace, you can also apply an 80's solution, just #define NOGDI before #including QOpenGLWindow, like this:

    #include <QMainWindow>
    #define NOGDI
    #include <QOpenGLWindow>
    
    class Rectangle
    {
    public:
        int r;
    };
    

    (This assumes you're not using any lowlevel Windows GDI functions in your cpp code)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.