Hello,
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(QMouseEvent *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.)