我有一个问题,我的Rectangle类没有被视为一个类型。我已经包含了正确的标题,所以我很困惑。
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_Hglwidget.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;
};很抱歉加载了这么多代码...确切的错误是'Rectangle‘不是类型glwidget.cpp行85
有谁知道为什么在glwidget.cpp中,尽管我包含了"Shapes.h“,但它不会将矩形视为一个类型?提前感谢!
发布于 2010-09-30 05:09:38
我要说的是,它与Shape中的虚函数有关,而不是像g++ undefined reference to typeinfo中那样定义。我出现这个奇怪错误的机器使用的Qt版本比我个人机器上的旧版本,而我的个人电脑对此代码没有任何问题。
谢谢大家的建议,但我还是把这个放在一边吧。
发布于 2010-09-29 06:24:51
这有点冒险,但是你确定你在GLWidget代码中正确地使用了moc吗?也就是说,您是否已经将#include "glwidget.moc添加到.cpp文件或将其包含在您的构建系统中(qmake知道会为您完成此操作),以及首先运行moc。我之所以提到这一点,是因为很久以前就忘记这样做了,这导致我看到了一堆与类型相关的难以理解的警告和错误。
发布于 2010-09-29 06:33:13
也许在GLWidget的祖先中的某个地方有一个叫做Rectangle的方法或成员,但其中有一个混淆。请参阅有关GLWidget及其祖先的文档
https://stackoverflow.com/questions/3817410
复制相似问题