Hi.
I try to compile the example of the QObject derived class "Counter" on
http://doc.trolltech.com/4.1/signalsandslots.html
but I only get this error:
g++ -c -pipe -O2 -march=athlon-xp -pipe -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/doc/qt-4.1.0-r2/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -o q.o q.cpp
g++ -o snor q.o -L/usr/lib/qt4 -lQtGui -L/usr/lib/mysql -L/usr/lib -L/usr/lib/qt4 -laudio -lXt -lpng -lSM -lICE -lXi -lXrender -lXrandr -lXcursor -lfreetype -lfontconfig -lXext -lX11 -lQtCore -lz -lm -ldl -lpthread
q.o: In function `main':
q.cpp:(.text+0x4b): undefined reference to `vtable for Counter'
q.cpp:(.text+0x69): undefined reference to `vtable for Counter'
q.cpp:(.text+0xbb): undefined reference to `vtable for Counter'
q.cpp:(.text+0xcd): undefined reference to `vtable for Counter'
q.cpp:(.text+0xe8): undefined reference to `vtable for Counter'
q.o:q.cpp:(.text+0xf7): more undefined references to `vtable for Counter' follow
q.o: In function `Counter::setValue(int)':
q.cpp:(.text+0x16): undefined reference to `Counter::valueChanged(int)'
collect2: ld returned 1 exit status
make: *** [snor] Error 1
g++ -c -pipe -O2 -march=athlon-xp -pipe -Wall -W -D_REENTRANT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/doc/qt-4.1.0-r2/mkspecs/linux-g++ -I. -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I. -I. -o q.o q.cpp
g++ -o snor q.o -L/usr/lib/qt4 -lQtGui -L/usr/lib/mysql -L/usr/lib -L/usr/lib/qt4 -laudio -lXt -lpng -lSM -lICE -lXi -lXrender -lXrandr -lXcursor -lfreetype -lfontconfig -lXext -lX11 -lQtCore -lz -lm -ldl -lpthread
q.o: In function `main':
q.cpp:(.text+0x4b): undefined reference to `vtable for Counter'
q.cpp:(.text+0x69): undefined reference to `vtable for Counter'
q.cpp:(.text+0xbb): undefined reference to `vtable for Counter'
q.cpp:(.text+0xcd): undefined reference to `vtable for Counter'
q.cpp:(.text+0xe8): undefined reference to `vtable for Counter'
q.o:q.cpp:(.text+0xf7): more undefined references to `vtable for Counter' follow
q.o: In function `Counter::setValue(int)':
q.cpp:(.text+0x16): undefined reference to `Counter::valueChanged(int)'
collect2: ld returned 1 exit status
make: *** [snor] Error 1
To copy to clipboard, switch view to plain text mode
What's wrong?
Here is the code:
#include <QObject>
{
Q_OBJECT
public:
Counter() { m_value = 0; }
int value() const { return m_value; }
public slots:
void setValue(int value);
signals:
void valueChanged(int newValue);
private:
int m_value;
};
void Counter::setValue(int value)
{
if (value != m_value) {
m_value = value;
emit valueChanged(value);
}
}
int main()
{
Counter a, b;
QObject::connect(&a,
SIGNAL(valueChanged
(int)),
&b, SLOT(setValue(int)));
a.setValue(12); // a.value() == 12, b.value() == 12
b.setValue(48); // a.value() == 12, b.value() == 48
}
#include <QObject>
class Counter : public QObject
{
Q_OBJECT
public:
Counter() { m_value = 0; }
int value() const { return m_value; }
public slots:
void setValue(int value);
signals:
void valueChanged(int newValue);
private:
int m_value;
};
void Counter::setValue(int value)
{
if (value != m_value) {
m_value = value;
emit valueChanged(value);
}
}
int main()
{
Counter a, b;
QObject::connect(&a, SIGNAL(valueChanged(int)),
&b, SLOT(setValue(int)));
a.setValue(12); // a.value() == 12, b.value() == 12
b.setValue(48); // a.value() == 12, b.value() == 48
}
To copy to clipboard, switch view to plain text mode
Bookmarks