0

我正在 Qt4 中构建一个小游戏。这是代码:

液晶控制.h:

#ifndef LCDCONTROL_H
#define LCDCONTROL_H



#include <QLCDNumber>
#include <QtGui>
#include <QWidget>

class lcdControl: public QWidget
{
    Q_OBJECT

public:
    lcdControl(QWidget *parent=0);

signals:
    void valueChanged(int newValue);

public slots:
    void setValue(int value);
    //void setRange(int minValue, int maxValue);
    //void quit();

private:
    QSlider *slider;
    //QPushButton *quitbutton;

};


#endif

液晶控制.cpp:

#include <QLCDNumber>
#include <QSlider>
#include <QGridLayout>

#include "lcdcontrol.h"

lcdControl::lcdControl(QWidget *parent): QWidget(parent)
{

    QLCDNumber *lcd = new QLCDNumber(2);                //2 for 2 digits
    lcd->setSegmentStyle(QLCDNumber::Filled);

    slider = new QSlider(Qt::Horizontal);
    slider->setRange(0,99);
    slider->setValue(0);

    connect(slider,SIGNAL(valueChanged(int)),lcd,SLOT(display(int)));   
    connect(slider,SIGNAL(valueChanged(int)),this,SIGNAL(valueChanged(int)));   

    QVBoxLayout *layout = new QVBoxLayout;
    layout->addWidget(lcd);
    layout->addWidget(slider);
    setLayout(layout);

    setFocusProxy(slider);
}

void lcdControl::setValue(int value)
{
    slider->setValue(value);
}

绘图.h:

#ifndef DRAWING_H
#define DRAWING_H

#include <QPushButton>

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

private:
    QPushButton *quitbutton;
};


#endif

绘图.cpp:

#include <QVBoxLayout>
#include <QGridLayout>
#include <QtGui>
#include "drawing.h"
#include "lcdcontrol.h"

drawingClass::drawingClass(QWidget *parent): QWidget(parent)
{

    quitbutton = new QPushButton(tr("&Quit!"));
    connect(quitbutton,SIGNAL(clicked()),qApp,SLOT(quit()));

    lcdControl *angle = new lcdControl;
    lcdControl *force = new lcdControl;

    QVBoxLayout *leftlayout = new QVBoxLayout;
    leftlayout->addWidget(angle);
    leftlayout->addWidget(force);

    QGridLayout *mainlayout = new QGridLayout;
    mainlayout->addWidget(quitbutton,0,0);
    mainlayout->addLayout(leftlayout);
    mainlayout->setColumnStrech(1,10);
    setLayout(mainlayout);
}

主文件

#include <QApplication>
#include <QtGui>
#include "drawing.h"

int main(int argc, char **argv)
{
    QApplication app(argc,argv);
    drawingClass ob;
    ob.resize(500,200);
    ob.show();
    return app.exec();
}

错误是:

main.cpp:(.text.startup+0x28): undefined reference to `drawingClass::drawingClass(QWidget*)'
main.cpp:(.text.startup+0x6e): undefined reference to `vtable for drawingClass'
main.cpp:(.text.startup+0x77): undefined reference to `vtable for drawingClass'
main.cpp:(.text.startup+0xb4): undefined reference to `vtable for drawingClass'
main.cpp:(.text.startup+0xbd): undefined reference to `vtable for drawingClass'
collect2: ld returned 1 exit status

这个你能帮我吗。

4

1 回答 1

2

您尚未为链接器提供drawingClass构造函数的定义。

您很可能忘记将该 .cpp 文件添加到项目中。

于 2012-10-06T10:32:50.443 回答