2011-11-29 2 views
2

저는 Qt 초보자이며이 프로그램이 제 첫 Qt 프로그램입니다. 저는 교과서에서 프로그램을 거의 복사했지만 작동하지 않았습니다. 나는 어리석은 실수를 저 지르지 않으면 안된다고 생각하지만 답변을 구글로 풀어 낼 수는 없다. 아래에 코드와 컴파일 결과를 게시합니다. 감사.오류 : ISO C++에서 유형이없는 'mousePressEvent'선언을 금지합니다.

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]); 

} 

MAIN.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*) 

답변

6

glface.cpp에 함수 정의의 모든 누락 된 반환 형식 : 등

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

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

...

+0

는'glface :: glface' 나에게 생성자처럼 엄청 많이 보이는 ... 당신은 확실 그 시작 부분에'무효 '를 원하십니까? +1 당신이 문제를 발견했기 때문에. –

+0

죄송합니다. 잘못된 예를 선택했습니다. 잠시 후에 고칠 것입니다. 지적 해 주셔서 감사합니다. – Mysticial

+0

내 어리석은 실수를 지적 해 주셔서 감사합니다! – EliteMyth

관련 문제