我正在Qt4中建立一个小游戏。这是代码:
lcdcontrol.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
lcdcontrol.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);
}
drawing.h:
#ifndef DRAWING_H
#define DRAWING_H
#include <QPushButton>
class drawingClass : public QWidget
{
Q_OBJECT
public:
drawingClass(QWidget *parent=0);
private:
QPushButton *quitbutton;
};
#endif
drawing.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);
}
的main.cpp
#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
请帮我解决这个问题。
答案 0 :(得分:2)
你没有为链接器提供drawingClass
构造函数的定义。
很可能您忘了将.cpp文件添加到项目中。