You are not logged in.

1

Friday, March 16th 2012, 10:20pm

undefined reference to `vtable for CannonField'

I am new with QT, can someone point me what I am missing in my makefile.


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
g++ `pkg-config --cflags --libs QtOpenGL QtGui QtCore` -c cannonfield.cpp -o cannonfield.o
g++ `pkg-config --cflags --libs QtOpenGL QtGui QtCore` -c lcdrange.cpp -o lcdrange.o
g++ `pkg-config --cflags --libs QtOpenGL QtGui QtCore` main.cpp -o main cannonfield.o lcdrange.o
cannonfield.o: In function `CannonField::CannonField(QWidget*)':
cannonfield.cpp:(.text+0x39): undefined reference to `vtable for CannonField'
cannonfield.cpp:(.text+0x43): undefined reference to `vtable for CannonField'
cannonfield.o: In function `CannonField::CannonField(QWidget*)':
cannonfield.cpp:(.text+0x133): undefined reference to `vtable for CannonField'
cannonfield.cpp:(.text+0x13d): undefined reference to `vtable for CannonField'
cannonfield.o: In function `CannonField::setAngle(int)':
cannonfield.cpp:(.text+0x244): undefined reference to `CannonField::angleChanged(int)'
cannonfield.o: In function `CannonField::tr(char const*, char const*)':
cannonfield.cpp:(.text._ZN11CannonField2trEPKcS1_[CannonField::tr(char const*, char const*)]+0x1e): undefined reference to `CannonField::staticMetaObject'
lcdrange.o: In function `LCDRange::LCDRange(QWidget*)':
lcdrange.cpp:(.text+0x3a): undefined reference to `vtable for LCDRange'
lcdrange.cpp:(.text+0x44): undefined reference to `vtable for LCDRange'
lcdrange.o: In function `LCDRange::LCDRange(QWidget*)':
lcdrange.cpp:(.text+0x2be): undefined reference to `vtable for LCDRange'
lcdrange.cpp:(.text+0x2c8): undefined reference to `vtable for LCDRange'
collect2: ld returned 1 exit status
make: *** [main] Error 1


Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
CC 	= gcc
CPP	= g++
CFLAGS = `pkg-config --cflags --libs QtOpenGL QtGui QtCore`
OBJECTS = cannonfield.o lcdrange.o
PROGS = main

all: $(OBJECTS) $(PROGS)

cannonfield.o:
	$(CPP) $(CFLAGS) -c cannonfield.cpp -o cannonfield.o

lcdrange.o:
	$(CPP) $(CFLAGS) -c lcdrange.cpp -o lcdrange.o

main:
	$(CPP) $(CFLAGS) main.cpp -o main $(OBJECTS) lcdrange.h cannonfield.h

clean :
	rm $(PROGS) $(OBJECTS)

This post has been edited 1 times, last edit by "apostle1978" (Mar 16th 2012, 10:28pm)


2

Friday, March 16th 2012, 11:05pm

show your cpp and .h files in their completeness

where are your moc files? Are you using qmake or moc at all?
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 5 times, last edit by "Amleto" (Mar 16th 2012, 11:12pm)


3

Saturday, March 17th 2012, 5:02am

source code files

I am trying to compile with make on my Debian Lenny system the "Preparing For Battle example" found here

[url]http://doc.trolltech.com/4.3/tutorial-t8.html
[/url]


I am not using qmake or moc files.



cannonfield.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <QPainter>

 #include "cannonfield.h"

 CannonField::CannonField(QWidget *parent)
 : QWidget(parent)
 {
 currentAngle = 45;
 setPalette(QPalette(QColor(250, 250, 200)));
 setAutoFillBackground(true);
 }

 void CannonField::setAngle(int angle)
 {
 if (angle < 5)
     angle = 5;
 if (angle > 70)
     angle = 70;
 if (currentAngle == angle)
     return;
 currentAngle = angle;
 update();
 emit angleChanged(currentAngle);
 }

 void CannonField::paintEvent(QPaintEvent * /* event */)
 {
 QPainter painter(this);
 painter.drawText(200, 200,
                  tr("Angle = ") + QString::number(currentAngle));
 }


cannonfield.h

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef CANNONFIELD_H
 #define CANNONFIELD_H

 #include <QWidget>

 class CannonField : public QWidget
 {
 Q_OBJECT

 public:
 CannonField(QWidget *parent = 0);

 int angle() const { return currentAngle; }

 public slots:
 void setAngle(int angle);

 signals:
 void angleChanged(int newAngle);

 protected:
 void paintEvent(QPaintEvent *event);

 private:
 int currentAngle;
 };

 #endif


lcdrange.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <QLCDNumber>
 #include <QSlider>
 #include <QVBoxLayout>

 #include "lcdrange.h"

 LCDRange::LCDRange(QWidget *parent)
 : QWidget(parent)
 {
 QLCDNumber *lcd = new QLCDNumber(2);
 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);
 }

 int LCDRange::value() const
 {
 return slider->value();
 }

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

 void LCDRange::setRange(int minValue, int maxValue)
 {
 if (minValue < 0 || maxValue > 99 || minValue > maxValue) {
     qWarning("LCDRange::setRange(%d, %d)\n"
              "\tRange must be 0..99\n"
              "\tand minValue must not be greater than maxValue",
              minValue, maxValue);
     return;
 }
 slider->setRange(minValue, maxValue);
 }


lcdrange.h

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#ifndef LCDRANGE_H
 #define LCDRANGE_H

 #include <QWidget>

 class QSlider;

 class LCDRange : public QWidget
 {
 Q_OBJECT

 public:
 LCDRange(QWidget *parent = 0);

 int value() const;

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

 signals:
 void valueChanged(int newValue);

 private:
 QSlider *slider;
 };

 #endif


main.cpp

Source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <QApplication>
 #include <QFont>
 #include <QGridLayout>
 #include <QPushButton>

 #include "cannonfield.h"
 #include "lcdrange.h"

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

 MyWidget::MyWidget(QWidget *parent)
 : QWidget(parent)
 {
 QPushButton *quit = new QPushButton(tr("Quit"));
 quit->setFont(QFont("Times", 18, QFont::Bold));

 connect(quit, SIGNAL(clicked()), qApp, SLOT(quit()));

 LCDRange *angle = new LCDRange;
 angle->setRange(5, 70);

 CannonField *cannonField = new CannonField;

 connect(angle, SIGNAL(valueChanged(int)),
         cannonField, SLOT(setAngle(int)));
 connect(cannonField, SIGNAL(angleChanged(int)),
         angle, SLOT(setValue(int)));

 QGridLayout *gridLayout = new QGridLayout;
 gridLayout->addWidget(quit, 0, 0);
 gridLayout->addWidget(angle, 1, 0);
 gridLayout->addWidget(cannonField, 1, 1, 2, 1);
 gridLayout->setColumnStretch(1, 10);
 setLayout(gridLayout);

 angle->setValue(60);
 angle->setFocus();
 }

 int main(int argc, char *argv[])
 {
 QApplication app(argc, argv);
 MyWidget widget;
 widget.setGeometry(100, 100, 500, 355);
 widget.show();
 return app.exec();
 }

4

Saturday, March 17th 2012, 11:03am

you cant compile code that inherits qt classes without letting moc tool/qmake create moc cpp files that you must compile and link.

Pleaes read up on how to build qt projects for your system.

http://doc.qt.nokia.com/4.7-snapshot/gettingstartedqt.html
If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

This post has been edited 1 times, last edit by "Amleto" (Mar 17th 2012, 11:10am)


5

Saturday, March 17th 2012, 12:19pm

Thanks

Thanks so much for pointing me in the correct direction.
I really appreciate you taking the time.

:)