Assignment 2 questuin 1

Posted by Anonymous User
Announcements Last Post
SoC Curricula 09/30/2017 01:08PM
Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
School of Computing Short Learning Programmes 11/24/2014 08:37AM
Unisa contact information 07/28/2011 01:28PM
 Re: Assignment 2 questuin 1 August 29, 2011 10:10AM Registered: 14 years ago Posts: 1,424 Rating: 0
Post more of the error list. That error message is not enough.
 Re: Assignment 2 questuin 1 August 29, 2011 03:45PM Registered: 9 years ago Posts: 77 Rating: 0
The error list is:

In function 'Booking':
undefined reference to 'vtable for Booking'
undefined reference to 'vtable for Booking'
collect2: Id returned 1 exit status

Language: C++ (QT)#include "person.h"
#include <QDate>
#include <QString>

class Booking
{
public:
Booking(const Person& c, QDate a, QDate d);
QString toString();
virtual double rate();
bool booked(QDate d);

double SINGLE_PPPN;
double SHARING_PPPN;

private:
Person m_Contact;
QDate m_ArrivalDate;
QDate m_DepartureDate;
};

and the source code (skeleton)

Language: C++ (QT)Booking::Booking(Person c, QDate arrive, QDate d) :  m_Contact(c), m_ArrivalDate(a), m_DepartureDate(d)
{
SINGLE_PPPN = 200.00;
SHARING_PPPN = 100.00;
}

bool Booking::booked(QDate d)
{
return((d >= m_ArrivalDate) && (d < m_DepartureDate));
}
 Re: Assignment 2 questuin 1 August 29, 2011 06:51PM Registered: 9 years ago Posts: 198 Rating: 0
I think I know what you did wrong....

The compiler first looks through your Booking class, sees that it needs a Person instance, but because the Person class comes after the Booking class in the .pro folder, it has not been "seen" yet.

This happens because you (Qt) are using contiguous memory. Qt creates a virtual table of the start addresses of each of your classes. If your classes are out of order, Qt won't be able to find them in memory and post a virtual table warning.

In other words in your .pro folder, switch Booking.h and Person.h around, save and REBUILD / CLEAN your project.
Just make sure that if you have more classes inheriting from other classes, that the classes are ordered logically.

Hope that helps.
Regards
Benjamin
 Re: Assignment 2 questuin 1 August 29, 2011 07:27PM Registered: 14 years ago Posts: 1,424 Rating: 0
Nope, that's not it Ben. QMake will convert the .pro file to a MAKEFILE and then it gets processed by MinGW like any other ordinary C++ project.

If you look at the header file of the Booking class' definition, you'll see there's a #include "Person.h". This means that the Person.h file will be opened by then precompiler and the Person class definition will be seen before the definition of the Booking class.

You're partially correct in saying that virtual function table is created at the beginning of the class instance but only if you have virtual functions. The virtual function table is the core behind-the-scenes data structure that makes polymorphism work in C++. It's quite ingenious if you want to research it further. It works on the fact that a function can be treated like a pointer variable and thus, the code's address can be stored in a data structure.

Whenever you're looking at an 'undefined reference' error, it's a linker error, meaning that compilation has succeeded. If the Person class was unknown, the compiler would have thrown a hissy-fit long time before and aborted compilation a long time before the linker even got a chance to see anything.

@sly_fox

What bothers me is that the error says:

Quote

In function 'Booking':

There is no function "Booking". There's a class Booking and it has four functions

Booking:: Booking
Booking:: toString
Booking:: rate
Booking:: booked

If you didn't do a copy/paste when posting the error message, please make sure in which function the error is being generated. If you did indeed do a copy/paste, I suspect that you didn't put the Booking:: scope in front of a function definition. I suspect that it might have something to do with the Booking:: rate() function, since it's virtual.

i.e. you might have put

bool rate(){
...
}

bool Booking:: rate(){
...
}

Oh! One more thing. Make sure you're not trying to call a virtual function from inside a constructor. It just won't work the way you think.
 Re: Assignment 2 questuin 1 August 29, 2011 07:28PM Registered: 10 years ago Posts: 3,496 Rating: 1
You can get a more complete error message than that, too. Look at the tabs on the output window, and you'll see you can select "Compiler Output". Click on that and the offending class should appear there.
 Re: Assignment 2 questuin 1 August 29, 2011 09:30PM Registered: 9 years ago Posts: 77 Rating: 0
the error is in booking.cpp
 Re: Assignment 2 questuin 1 August 29, 2011 09:32PM Registered: 9 years ago Posts: 77 Rating: 0
And it says in function Booking on the compiler output
 Re: Assignment 2 questuin 1 August 29, 2011 09:38PM Registered: 9 years ago Posts: 77 Rating: 0
Here's the compiler output

Running build steps for project A2Q1...
Configuration unchanged, skipping qmake step.
Starting: "E:/Qt/2010.04/mingw/bin/mingw32-make.exe" -w
mingw32-make: Entering directory E:/unisa/cos2614/A2Q1-build-desktop'
E:/Qt/2010.04/mingw/bin/mingw32-make -f Makefile.Debug
mingw32-make[1]: Entering directory E:/unisa/cos2614/A2Q1-build-desktop'
g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -I"..\..\..\Qt\2010.04\qt\include\QtCore" -I"..\..\..\Qt\2010.04\qt\include\QtGui" -I"..\..\..\Qt\2010.04\qt\include" -I"..\..\..\Qt\2010.04\qt\include\ActiveQt" -I"debug" -I"..\A2Q1" -I"." -I"..\..\..\Qt\2010.04\qt\mkspecs\win32-g++" -o debug\main.o ..\A2Q1\main.cpp
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -Wl,-subsystem,console -mthreads -Wl -o debug\A2Q1.exe debug/booking.o debug/single.o debug/sharing.o debug/person.o debug/main.o -L"e:\Qt\2010.04\qt\lib" -lQtGuid4 -lQtCored4
mingw32-make[1]: Leaving directory E:/unisa/cos2614/A2Q1-build-desktop'
mingw32-make: Leaving directory E:/unisa/cos2614/A2Q1-build-desktop'
debug/booking.o: In function Booking':
E:\unisa\cos2614\A2Q1-build-desktop/../A2Q1/booking.cpp:5: undefined reference to vtable for Booking'
E:\unisa\cos2614\A2Q1-build-desktop/../A2Q1/booking.cpp:5: undefined reference to vtable for Booking'
debug/single.o:single.cpp.text\$_ZN7BookingD2Ev[Booking::~Booking()]+0xb): undefined reference to vtable for Booking'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\A2Q1.exe] Error 1
mingw32-make: *** [debug] Error 2
The process "E:/Qt/2010.04/mingw/bin/mingw32-make.exe" exited with code %2.
Error while building project A2Q1 (target: Desktop)
When executing build step 'Make'
 Re: Assignment 2 questuin 1 August 29, 2011 10:03PM Registered: 10 years ago Posts: 3,496 Rating: 1
I can give you some of the silly mistakes I've made regularly:

1. Are you sure your declaration is inside the class braces? (Once I managed to somehow declare a function after the final ";", and it kept me busy a while).

2. Are you sure you used the proper :: in the ".cpp"?

No I don't have a hunch that that's where your problem lies; I've just run out of ideas.

Ben's idea of rebuilding is worth trying. Sometimes Qt stays stuck on your earlier version, and rebuilding provides a "magic fix". It can't hurt, right? Or did you try it to no avail?
 Re: Assignment 2 questuin 1 August 29, 2011 10:23PM Registered: 9 years ago Posts: 77 Rating: 0
Thanks slow eddy, robanaurochs, BenVP

I'll try starting from scratch and see what happens.
 Re: Assignment 2 questuin 1 August 29, 2011 10:31PM Registered: 14 years ago Posts: 1,424 Rating: 0
Quote

E:\unisa\cos2614\A2Q1-build-desktop/../A2Q1/booking.cpp:5: undefined reference to `vtable for Booking'

This means that the error is on line 5 of booking.cpp

 Re: Assignment 2 questuin 1 August 29, 2011 10:53PM Registered: 9 years ago Posts: 77 Rating: 0
5: Booking::Booking(Person c, QDate arrive, QDate d) : m_Contact(c), m_ArrivalDate(a), m_DepartureDate(d)
6: {
7: SINGLE_PPPN = 200.00;
8: SHARING_PPPN = 100.00;
9: }
 Re: Assignment 2 questuin 1 August 30, 2011 11:15AM Registered: 10 years ago Posts: 3,496 Rating: 1
Booking is not a subclass of anything? It doesn't sound like it would be. If it is, you haven't initialised the constructor of the base class.
 Re: Assignment 2 questuin 1 August 30, 2011 12:20PM Registered: 9 years ago Posts: 198 Rating: 0
@robanaurochs

Thanks for the correction.
You're right the virtual function table is an amazing thing, busy Googling it now...
 Re: Assignment 2 questuin 1 August 30, 2011 12:37PM Registered: 14 years ago Posts: 1,424 Rating: 0
What do all the constructors of Single look like?
 Re: Assignment 2 questuin 1 August 30, 2011 12:40PM Registered: 14 years ago Posts: 1,424 Rating: 0
OH!!!!!

You're deriving Single and Shared from Booking.

Give Booking an empty virtual destructor. This is a very important rule of thumb that's very seldomly documented. Whenever deriving a class, the base class should have a virtual destructor, otherwise the wrong destructor might get called.

This might not be the problem, but it's a very important thing to know.
 Re: Assignment 2 questuin 1 August 30, 2011 12:45PM Registered: 14 years ago Posts: 1,424 Rating: 0
@Ben

When looking up function pointers, ignore the highly complex examples. The syntax allows for some strange extremes that can boggle your mind and teachers of C++ love showing off with highly elaborate function pointer definitions but, to be honest, I've never seen any in real code that come even close to the extremes that you find in tutorials and textbooks.
 Re: Assignment 2 questuin 1 August 30, 2011 12:48PM Registered: 10 years ago Posts: 3,496 Rating: 1
Look carefully at your second parameter, and then look at the second init in the list. If you've given us what you literally wrote, I think that will solve your problem.

Edit: Yes, that looks like it's been there under all our noses all the time.
 Re: Assignment 2 questuin 1 August 30, 2011 01:34PM Registered: 14 years ago Posts: 1,424 Rating: 0
@Eddy

I think this was a retype and not a copy/paste. The errors you see would have been picked up by the compiler before they got to the linker.
 Re: Assignment 2 questuin 1 August 30, 2011 01:38PM Registered: 10 years ago Posts: 3,496 Rating: 1
OK, yes I forgot. If you get a linker error the compiler is happy with what you sent it.
 Re: Assignment 2 questuin 1 August 31, 2011 01:48PM Registered: 8 years ago Posts: 1 Rating: 0
I had the vtable error and solved it by changing the rate() function to a pure virtual function.
Sorry, only registered users may post in this forum.