c++ - 错误 :ISO C++ forbids declaration of ‘mousePressEvent’ with no type

标签 c++ qt

我是 Qt 的初学者,这是我的第一个 Qt 程序。我差点从我的课本上复制一个程序,但它没有用。我想我一定犯了一个愚蠢的错误,但我无法用谷歌搜索出答案。我将在下面发布代码和编译结果。谢谢。

glface.h:

#ifndef GLFACE_H

#define GLFACE_H

#include<QWidget>

#include<QPoint>

class glface: public QWidget
{
Q_OBJECT
public:
glface(QWidget *parent = 0);

protected:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);

private slots:
void clear();

void toget();

void drawline(QPainter *painter);


private:
QTimer *recordtimer;

static bool got; //the flag for the timer

bool startdraw;

QPoint lastpoint;

QPoint point[100];

static int pointcount;
};

#endif // GLFACE_H

glface.cpp

#include <QtGui>
#include "glface.h"


glface::glface(QWidget *parent):QWidget(parent)
{
recordtimer= new QTimer(this);
connect(recordtimer,SIGNAL(timeout()),this,SLOT(toget()));


}
glface::mousePressEvent(QMouseEvent *event)
{
if (event->button==Qt::LeftButton )
{
    startdraw=true;
    point[pointcount]=event->pos();
    pointcount++;
    recordtimer->start(1000);

}


}
glface::toget()
{
got=true;
}

glface::mouseMoveEvent(QMouseEvent *event)
{
if (event->button()==Qt::LeftButton && event->pos()!=lastpoint && startdraw && got)
{
    point[pointcount]=event->pos();
    pointcount++;
    got=false;
    drawline(&painter);
}
}
glface::mouseReleaseEvent(QMouseEvent *event)
{
point[pointcount]=event->pos();
pointcount++;
startdraw=false;
recordtimer->stop();
got=false;
pointcount=0;
}

glface::paintEvent(QPaintEvent *event)
{
QPainter painter(this);
painter.setRenderHint(QPainter::Antialiasing, true);
painter.setPen(QPen(Qt::black, 15, Qt::SolidLine, Qt::RoundCap,
Qt::MiterJoin));
painter.setBackground(Qt::white);
painter.setWindow(0,0,400,300);
}
glface::drawline(QPainter *painter)
{
if (pointcount>1)
    painter->drawLine(point[pointcount-1],point[pointcout-2]);

}

主要.cpp

#include <QApplication>
#include <QtCore>
#include "glface.h"
int main(int argc,char *argv[])
{
QApplication app(argc, argv);
glface face;
face.show();
return app.exec();

}

编译结果:

glface.cpp:12: error: ISO C++ forbids declaration of ‘mousePressEvent’ with no type
glface.cpp:12: error: prototype for ‘int glface::mousePressEvent(QMouseEvent*)’ does not     match any in class ‘glface’
glface.h:15: error: candidate is: virtual void glface::mousePressEvent(QMouseEvent*)
glface.cpp:25: error: ISO C++ forbids declaration of ‘toget’ with no type
glface.cpp:25: error: prototype for ‘int glface::toget()’ does not match any in class ‘glface’
glface.h:20: error: candidate is: void glface::toget()
glface.cpp:30: error: ISO C++ forbids declaration of ‘mouseMoveEvent’ with no type
glface.cpp:30: error: prototype for ‘int glface::mouseMoveEvent(QMouseEvent*)’ does not match any in class ‘glface’
glface.h:16: error: candidate is: virtual void glface::mouseMoveEvent(QMouseEvent*)
glface.cpp:40: error: ISO C++ forbids declaration of ‘mouseReleaseEvent’ with no type
glface.cpp:40: error: prototype for ‘int glface::mouseReleaseEvent(QMouseEvent*)’ does not match any in class ‘glface’
glface.h:17: error: candidate is: virtual void glface::mouseReleaseEvent(QMouseEvent*)
glface.cpp:50: error: ISO C++ forbids declaration of ‘paintEvent’ with no type
glface.cpp:50: error: prototype for ‘int glface::paintEvent(QPaintEvent*)’ does not match any in class ‘glface’
glface.h:14: error: candidate is: virtual void glface::paintEvent(QPaintEvent*)
glface.cpp:59: error: ISO C++ forbids declaration of ‘drawline’ with no type
glface.cpp:59: error: prototype for ‘int glface::drawline(QPainter*)’ does not match any in class ‘glface’
glface.h:21: error: candidate is: void glface::drawline(QPainter*)

最佳答案

glface.cpp 中的所有函数定义都缺少返回类型:

void glface::mousePressEvent(QMouseEvent *event)
^^^^ missing

void glface::mouseMoveEvent(QMouseEvent *event)
^^^^ missing

等...

关于c++ - 错误 :ISO C++ forbids declaration of ‘mousePressEvent’ with no type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8305282/

相关文章:

c++ - 输出一个15位数字

c++ - 具有未使用模板参数的函数模板

c++ - VS2013 调试器无法再解析包含源代码路径的系统环境变量

c++ - 对 'cvCreateKalman' 的 undefined reference

c++ - 无法在 C++ 中创建结构 vector

c++ - 强制 GCC 不要优化掉一个未使用的变量?

c++ - 从不同的线程、不同的函数写入(登录)同一个文件?

c++ - 在 QTabWidget 上放置一个关闭按钮

c++ - 如何在 QML 中使用模型?

c++ - Qt 链接器错误