PDA

View Full Version : Why this error?



killrazor
18th January 2010, 00:38
Hi,
I'm trying to compile this code


#include <QApplication>
#include <QLCDNumber>
#include <QPushButton>
#include <QVBoxLayout>
#include <QWidget>

class LCDCounter : public QLCDNumber{
Q_OBJECT

private:
int counter;
public:
LCDCounter(QWidget *parent = 0);
public slots:
void increase(void);

};

LCDCounter::LCDCounter(QWidget *parent)
: QLCDNumber(parent)
{
counter = 0;
setSegmentStyle(QLCDNumber::Filled);
display(0);
}

void LCDCounter::increase(){
display(counter++);
}

class MyWidget : public QWidget
{
QPushButton *clickMe;
LCDCounter *counter;

public:
MyWidget(QWidget *parent = 0);
};

MyWidget::MyWidget(QWidget *parent)
: QWidget(parent)
{
clickMe = new QPushButton("Click me!");
counter = new LCDCounter();
connect(clickMe,SIGNAL(clicked()),counter,SLOT(inc rease()));

QVBoxLayout *boxLayout = new QVBoxLayout;
boxLayout->addWidget(clickMe);
boxLayout->addWidget(counter);
setLayout(boxLayout);

}

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget w;
w.show();
return a.exec();
}



But this error is returned.



1>main.obj : error LNK2001: sÃ*mbolo externo "public: virtual struct QMetaObject const * __thiscall LCDCounter::metaObject(void)const " (?metaObject@LCDCounter@@UBEPBUQMetaObject@@XZ) unresolved
1>main.obj : error LNK2001: sÃ*mbolo externo "public: virtual void * __thiscall LCDCounter::qt_metacast(char const *)" (?qt_metacast@LCDCounter@@UAEPAXPBD@Z) unresolved
1>main.obj : error LNK2001: sÃ*mbolo externo "public: virtual int __thiscall LCDCounter::qt_metacall(enum QMetaObject::Call,int,void * *)" (?qt_metacall@LCDCounter@@UAEHW4Call@QMetaObject@@ HPAPAX@Z) unresolved
1>C:\Users\ferran\directo\QT_Tutorials\Debug\Tutoria l02.exe : fatal error LNK1120: 3 externos sin resolver


How do I guess the method that fails? What are happening?

Many thanks in advance.

ChrisW67
18th January 2010, 01:42
QLCDNumber is already a QObject so you don't require the Q_OBJECT macro in your derived class definition. The linker is getting confused looking in the wrong place for the tables used to support QObjects.

wagmare
18th January 2010, 07:37
but if he removes Q_OBJECT the connect will be failed ... at console he will receive
Object::connect: No such slot QLCDNumber::increase()

squidge
18th January 2010, 07:55
MOC isn't being run. If your using the QtCreator IDE, then use Build->Run QMake. Leave the Q_OBJECT in there, it's required.

wagmare
18th January 2010, 09:01
but in normal qt compilation ...

he should include "main.moc" in his main file

code will be


#include <QApplication>
#include <QtGui>

class LCDCounter : public QLCDNumber{
Q_OBJECT

private:
int counter;
public:
LCDCounter(QWidget *parent = 0)
:QLCDNumber(parent)
{
counter = 0;
setSegmentStyle(QLCDNumber::Filled);
display(0);
}
public slots:
void increase()
{
display(counter++);

}

};
class MyWidget : public QWidget
{
Q_OBJECT
public:
MyWidget()
: QWidget(0)
{
clickMe = new QPushButton("Click me!");
counter = new LCDCounter();
connect(clickMe,SIGNAL(clicked()),counter,SLOT(inc rease()));
QVBoxLayout *boxLayout = new QVBoxLayout;
boxLayout->addWidget(clickMe);
boxLayout->addWidget(counter);
setLayout(boxLayout);

}


private:
QPushButton *clickMe;
LCDCounter *counter;
};


#include "main.moc"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MyWidget w;
w.show();
return a.exec();
}

killrazor
18th January 2010, 09:55
Hi all and many thanks for your responses.


MOC isn't being run. If your using the QtCreator IDE, then use Build->Run QMake. Leave the Q_OBJECT in there, it's required.
I forgot to say that I'm working with Visual Studio 2008 and QT 4.6.
With the Visual Studio add-in, the project created with the assistant doesn't includes the moc file as wagmare says. I'll try the changes you are suggesting.

Thanks

wagmare
18th January 2010, 10:01
include "main.moc" above main and macro Q_OBJECT in MyWidget class .. your code is working fine ...