关于错误: error: variable ‘QApplication app’ has initializer but incomplete type

#include <QApplication>
#include <QMainWindow>
#include <QTextEdit>
#include <QKeyEvent>
#include <QDebug>

class MainWindow : public QMainWindow
 {
 public:
     MainWindow();

 protected:
     bool eventFilter(QObject *obj, QEvent *ev);

 private:
     QTextEdit *textEdit;
 };

 MainWindow::MainWindow()
 {
     textEdit = new QTextEdit;
     setCentralWidget(textEdit);

     textEdit->installEventFilter(this);
 }

 bool MainWindow::eventFilter(QObject *obj, QEvent *event)
 {
     if (obj == textEdit) {
         if (event->type() == QEvent::KeyPress) {
             QKeyEvent *keyEvent = static_cast<QKeyEvent*>(event);
             qDebug() << "Ate key press" << keyEvent->key();
             return true;
         } else {
             return false;
         }
     } else {
         // pass the event on to the parent class
         return QMainWindow::eventFilter(obj, event);
     }
 }

int main(int argc, char *argv[])
{
     QApplication app(argc, argv);
     app.setOrganizationName("c-cube");
     app.setApplicationName("Evt filter Example");
     MainWindow mainWin;
     mainWin.show();
     return app.exec();
}


 上边的代码,如果不细心,忘了 #include <QApplication>, 那么编译就会出现很不好理解的一个错误提示:

[wyh@ eventFilter 11:26:51]$make
g++ -c -pipe -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/lib64/qt4/mkspecs/linux-g++ -I. -I/usr/include/QtCore -I/usr/include/QtGui -I/usr/include -I. -I. -o editBox_evt.o editBox_evt.cpp
editBox_evt.cpp: In function ‘int main(int, char**)’:
editBox_evt.cpp:45:23: error: variable ‘QApplication app’ has initializer but incomplete type
make: *** [editBox_evt.o] Error 1
[wyh@ eventFilter 11:26:56]$

 

按理说,应该提示类似: error: ‘QTextEdit’ does not name a type (如果去掉 #include <QTextEdit> 的话)

 

可见,以后如果再遇到 类似 error: variable ‘QxxxClass  xxxvar’ has initializer but incomplete type 的提示, 检查下是否忘了 #include <QxxxClass>

 

这里有个别的例子: http://www.developer.nokia.com/Community/Discussion/showthread.php?225679-Variable-has-initializer-but-incomplete-type  使用的是QGraphicsPixmapItem类。

 

 

你可能感兴趣的:(关于错误: error: variable ‘QApplication app’ has initializer but incomplete type)