tags:

views:

85

answers:

4

Hi,

Im having a problem of my Rectangle class not being seen as a type. I've included the proper header, and so I am confused.

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 : Shape
{
    public:
        Rectangle(GLWidget *w, QPoint tl, QPoint br){
            glWidget = w;
            topLeft = tl;
            btmRight = 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;
        RGBColor color;
};

#endif // SHAPES_H

glwidget.cpp

#include <QtGui>
#include <QtOpenGL>

#include <math.h>
#include <stdio.h>
#include "glwidget.h"

#include "Shapes.h"

#ifndef GL_MULTISAMPLE
#define GL_MULTISAMPLE  0x809D
#endif

// ...   a bunch of code that doesn't need to be included

void GLWidget::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton){
        // do some drawing stuff
        QPoint mPos = event->pos();

        switch(drawmode)
        {
            case 1:
               currentShape = new Rectangle(this,mPos, mPos);  /***  This is the error ***/
        }

    }
}

glwidget.h

#ifndef AGLWIDGET_H
#define AGLWIDGET_H

#include <QGLWidget>

#include "Colors.h"

class Shape;

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;

};

Sorry for the load of code... the exact error is 'Rectangle' is not a type glwidget.cpp line 85

Anybody have an idea why it wouldn't be seeing Rectangle as a type in glwidget.cpp despite my including "Shapes.h"? Thanks in advance!

+1  A: 

This is a bit of a longshot, but are you sure you're using moc correctly in regards to the GLWidget code? IE, have you added #include "glwidget.moc to the .cpp file or included it in your build system (qmake knows to do this for you), as well as running moc first. I only mention this because forgetting to do this many moons ago caused me to see a pile of inscrutable type-related warnings and errors.

jkerian
+1  A: 

Perhaps somewhere in the ancestry of GLWidget there is a method or member called Rectangle and there is a confusion. See the documentation for GLWidget and its ancestors

frag
If that were the case, the error would be more like "no suitable method Rectangle::Rectangle(GLWidget, int, int)" or something, rather than flat out saying Rectangles don't exist.
cHao
GLWidget is a class created by the OP's code, by the way.
cHao
@cHao: It's not saying `Rectangle` doesn't exist, it's saying it's not a type, which could happen if it's a non-type name inherited from a base class. @wallacer: try using its fully-qualified name (`::Rectangle`), and see if that helps.
Mike Seymour
A: 

Looks like the compiler believes Rectangle is a template

Alex Emelianov
A: 

Well I'm gonna go with it had something to do with the virtual function within Shape not being defined as in http://stackoverflow.com/questions/307352/g-undefined-reference-to-typeinfo. The machine I had the strange error on is using an older version of Qt than I have on my personal machine, and my personal is having no issues with this code.

Thanks for the suggestions everyone, but I'm just gonna put this one to rest.

wallacer