2013-05-01 89 views
0

我刚刚与Qt合作过。这是一本书的例子。 我试图将代码与书中的代码进行比较。它看起来相同。无法确定代码无法执行的原因

你能帮我吗?

使用NetBeans显示如下:

"/usr/bin/make" -f nbproject/Makefile-Debug.mk QMAKE=/C/Qt/4.8.4/bin/qmake.exe SUBPROJECTS= .build-conf 
make[1]: Entering directory `/c/Documents and Settings/Deloitte/��� ���������/NetBeansProjects/HelloWorld1' 
/C/Qt/4.8.4/bin/qmake.exe VPATH=. -spec win32-g++ -o qttmp-Debug.mk nbproject/qt-Debug.pro 
mv -f qttmp-Debug.mk nbproject/qt-Debug.mk 
"/usr/bin/make" -f nbproject/qt-Debug.mk dist/Debug/MinGW_Qt-Windows/HelloWorld1.exe 
make[2]: Entering directory `/c/Documents and Settings/Deloitte/��� ���������/NetBeansProjects/HelloWorld1' 
g++ -mthreads -Wl,-subsystem,windows -o dist/Debug/MinGW_Qt-Windows/HelloWorld1.exe build/Debug/MinGW_Qt-Windows/main.o build/Debug/MinGW_Qt-Windows/moc_Counter.o -L'c:/Qt/4.8.4/lib' -lmingw32 -lqtmaind build/Debug/MinGW_Qt-Windows/HelloWorld1_resource_res.o -lQtGuid4 -lQtCored4 
build/Debug/MinGW_Qt-Windows/main.o: In function `Z5qMainiPPc': 
C:\Documents and Settings\Deloitte\��� ���������\NetBeansProjects\HelloWorld1/main.cpp:16: undefined reference to `Counter::Counter()' 
build/Debug/MinGW_Qt-Windows/moc_Counter.o: In function `ZN7Counter18qt_static_metacallEP7QObjectN11QMetaObject4CallEiPPv': 
C:\Documents and Settings\Deloitte\��� ���������\NetBeansProjects\HelloWorld1/moc_Counter.cpp:56: undefined reference to `Counter::slotInc()' 
collect2: ���������� ld ����������� � ����� �������� 1 
make[2]: *** [dist/Debug/MinGW_Qt-Windows/HelloWorld1.exe] Error 1 
make[2]: Leaving directory `/c/Documents and Settings/Deloitte/��� ���������/NetBeansProjects/HelloWorld1' 
make[1]: *** [.build-conf] Error 2 
make[1]: Leaving directory `/c/Documents and Settings/Deloitte/��� ���������/NetBeansProjects/HelloWorld1' 
make: *** [.build-impl] Error 2 


BUILD FAILED (exit value 2, total time: 1s) 

counter.h

#ifndef _Counter_h_ 
#define _Counter_h_ 

#include <QObject> 

class Counter:public QObject{ 
    Q_OBJECT 
    private: 
     int m_nValue; 
     public: 
      Counter(); 
     public slots: 
      void slotInc(); 
     signals: 
      void goodbye(); 
      void counterChanged(int); 
}; 

的main.cpp

#include <QtGui/QApplication> 
#include <QtGui> 
#include "Counter.h" 

int main(int argc, char *argv[]) { 
    QApplication app(argc, argv); 
    QLabel lbl("0"); 
    QPushButton cmd ("ADD"); 
    Counter counter; 
    lbl.show(); 
    cmd.show(); 

    QObject::connect(&cmd, SIGNAL(clicked()), 
      &counter, SLOT (slotInc())); 

    QObject::connect(&counter, SIGNAL(counterChanged(int)), 
      &lbl, SLOT(setNum(int))); 

    QObject::connect(&counter, SIGNAL (goodbye()), &app, SLOT(quit())); 


    return app.exec(); 
} 
+1

你的计数器的成员函数和构造函数的代码在哪里? – Mat 2013-05-01 10:41:18

回答

0

您的代码缺少的实际定义计数器cl ass成员函数,特别是Counter::Counter的构造函数和Counter::slotInc。你必须为它们提供实现。

Counter::goodbyeCounter::counterChanged是定义由Qt MOC提供的信号。

+0

嗯,是的,我错过了本书后面写的内容。请原谅我。 – Trts 2013-05-01 10:54:01