开发者

Qt ISO C++ forbids declaration without type - header file is included?

开发者 https://www.devze.com 2023-01-18 12:31 出处:网络
I\'m receiving the forbids declaration without type error in a Qt application I\'m working on.The problem is that I have included the header file which declares the class.It should be defined as a typ

I'm receiving the forbids declaration without type error in a Qt application I'm working on. The problem is that I have included the header file which declares the class. It should be defined as a type as far as I can tell. I tried forward declaration as well, but I'd like to use the methods of the class in this file and so I need the whole header file. code:

Shapes.h

#ifndef SHAPES_H
#define SHAPES_H

#include "Colors.h"
#include <QPoint>
#include "glwidget.h"

//class GLWidget;  

class Shape
{
    public:

        virtual void draw();
};

class Rectangle : public Shape
{
    public:
        Rectangle(GLWidget *w, QPoint tl, QPoint br);

        virtual void draw(){
              // top horizontal
            for(int i = topLeft.x(); i < btmRight.x(); i++){
                 glWidget->setPixel(i,topLeft.y(), color);
             }
        }

    private:
        QPoint topLeft,btmRight;
        GLWidget *glWidget;        /****  This is the Error line  ****/
        RGBColor color;
};

#endif // SHAPES_H

glwidget.h:

#ifndef AGLWIDGET_H
#define AGLWIDGET_H

#include <QGLWidget>

#include "Colors.h"
#include "Shapes.h"

class GLWidget : public QGLWidget
{
    Q_OBJECT

public:
    GLWidget(QWidget *parent = 0);
    ~GLWidget();

    QSize minimumSizeHint() const;
    QSize sizeHint() const;

    void setPixel(int x, int y, RGBColor c);

public slots:
    void setColor(RGBColor c);
    void setDrawRectangle();



protected:
    void initializeGL();
    void paintGL();
    void resizeGL(int width, int height);
    void mousePressEvent(QMouseEvent *event);
    void mouseMoveEvent(QM开发者_运维百科ouseEvent *event);

private:

    QPoint lastPos;
    QVector<QPoint> drawPoints;
    RGBColor paintColor;
    int drawmode;
    Shape *currentShape;

};


#endif

I don't see why I can't use my GLWidget in Shapes.h. The error is in Shapes.h on the line where I declare GLWidget *glWidget; If I use a forward declaration ie. class GLWidget; this error goes away but then I can't actually use the GLWidget methods as in Rectangle.draw()

Anyone have an idea why the compiler wouldn't see GLWidget as a type in Shapes.h?

Exact errors:

Shapes.h:20: error: expected ')' before '*' token

Shapes.h:31: error: ISO C++ forbids declaration of 'GLWidget' with no type

Shapes.h:31: error: expected ';' before '*' token

Shapes.h: In member function 'virtual void Rectangle::draw()':

Shapes.h:25: error: 'glWidget' undeclared (first use this function)

Shapes.h:25: error: (Each undeclared identifier is reported only once for each function it appears in.)


You have a circular dependency between your header files -- Shapes.h includes glwidget.h, which includes Shapes.h again. So, the second time the compiler tries to include glwidget.h, the include guard AGLWIDGET_H has been defined, so it doesn't include it again, and then the code in Shapes.h tries to use types that ought to have been declared but are not.

To fix it, you need to remove one of the dependencies and use forward declarations instead. Since glwidget.h doesn't actually use the Shape class beyond declaring a pointer member variable, you should remove its inclusion of Shapes.h:

glwidget.h:

#ifndef AGLWIDGET_H
#define AGLWIDGET_H

// do NOT include Shapes.h

class Shape;  // forward declaration

class GLWidget : public QGLWidget
{
    ...
    Shape *currentShape;
};

#endif

Shapes.h is the same as before:

#ifndef SHAPES_H
#define SHAPES_H

#include "glwidget.h"
...
#endif


The reason is the include order - you use a circular inclusion and only the include guards prevent you from looping the compiler.
You can resolve this in your particular case by forward-declaring the shape class in glWidget.h and removing the include for Shapes.h:

//#include "Shapes.h"
class Shape;
0

精彩评论

暂无评论...
验证码 换一张
取 消