473,386 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Beginner's problem: inheritence and vectors storing bookings

Hello

First off, I'm a student so I'm pretty new to C++, and therefore I have
probably made a stupid mistake somewhere.
Anyway Ive been trying to fix this 5 hours straight now, so i need a
little assistance.

What I'm trying to do
I am using inheritance to make some bookings for a marina, which are:
Booking
|
__________________________________
| | |
narrowBoat motorBoat sailingBoat

These bookings are being stored in a vector in the bookingRecords
collection class:
private :
vector< booking m_bookings;

I'm adding bookings like this:
void bookingRecords::addBooking(booking& v)
{
m_bookings.push_back( v ); //push to back of vector
}

Then iterating over them to run a method:
string bookingRecords::toString()
{
string retv("");
vector< booking >::iterator iter;
for (iter = m_bookings.begin(); iter !=m_bookings.end(); iter++)
{
retv+= iter->BtoString() + "\n";
}

return(retv);
}

The problem? When they get stored in this vector they lose their type
(e.g narrowBoat) and become a standard booking. I know this because when
I try to run a method (which is virtual in the booking class) it does
not run the correct method from one of the sub classes.

I did a bit of research and found a possible solution of using pointers
inside the vector instead of type booking, however I have not been able
to get this to work.

Any ideas on how to fix my problem? Am I totally in the wrong? I can
send my source on request if that helps.

Thanks for any assistance on this, it is much appreciated.
Dec 13 '06 #1
8 1928

"Mike Jolley" <jo**********@tesco.netwrote in message
news:_f******************@newsfe1-gui.ntli.net...
Hello

First off, I'm a student so I'm pretty new to C++, and therefore I have
probably made a stupid mistake somewhere.
Anyway Ive been trying to fix this 5 hours straight now, so i need a
little assistance.

What I'm trying to do
I am using inheritance to make some bookings for a marina, which are:
Booking
|
__________________________________
| | | narrowBoat motorBoat sailingBoat

These bookings are being stored in a vector in the bookingRecords
collection class:
private :
vector< booking m_bookings;

I'm adding bookings like this:
void bookingRecords::addBooking(booking& v)
{
m_bookings.push_back( v ); //push to back of vector
}

Then iterating over them to run a method:
string bookingRecords::toString()
{
string retv("");
vector< booking >::iterator iter;
for (iter = m_bookings.begin(); iter !=m_bookings.end(); iter++)
{
retv+= iter->BtoString() + "\n";
}

return(retv);
}

The problem? When they get stored in this vector they lose their type (e.g
narrowBoat) and become a standard booking. I know this because when I try
to run a method (which is virtual in the booking class) it does not run
the correct method from one of the sub classes.

I did a bit of research and found a possible solution of using pointers
inside the vector instead of type booking, however I have not been able to
get this to work.
That's correct: you need to store pointers instead. But you don't show the
code where you've tried to do that.

The first thing is to define the vector to store pointers (with booking*
instead of booking as the data type). Then you can push_back &v, instead of
simply v, in addBooking. You'll also need to modify the code that accesses
the items so that it's using pointers.

Try again, and if you have a specific problem, post the code that you're
having problems with, and tell us exactly what problem you're having. Then
we can suggest a fix.

-Howard

Dec 14 '06 #2
Howard wrote:
"Mike Jolley" <jo**********@tesco.netwrote in message
news:_f******************@newsfe1-gui.ntli.net...
>Hello

First off, I'm a student so I'm pretty new to C++, and therefore I have
probably made a stupid mistake somewhere.
Anyway Ive been trying to fix this 5 hours straight now, so i need a
little assistance.

What I'm trying to do
I am using inheritance to make some bookings for a marina, which are:
Booking
|
__________________________________
| | | narrowBoat motorBoat sailingBoat

These bookings are being stored in a vector in the bookingRecords
collection class:
private :
vector< booking m_bookings;

I'm adding bookings like this:
void bookingRecords::addBooking(booking& v)
{
m_bookings.push_back( v ); //push to back of vector
}

Then iterating over them to run a method:
string bookingRecords::toString()
{
string retv("");
vector< booking >::iterator iter;
for (iter = m_bookings.begin(); iter !=m_bookings.end(); iter++)
{
retv+= iter->BtoString() + "\n";
}

return(retv);
}

The problem? When they get stored in this vector they lose their type (e.g
narrowBoat) and become a standard booking. I know this because when I try
to run a method (which is virtual in the booking class) it does not run
the correct method from one of the sub classes.

I did a bit of research and found a possible solution of using pointers
inside the vector instead of type booking, however I have not been able to
get this to work.

That's correct: you need to store pointers instead. But you don't show the
code where you've tried to do that.

The first thing is to define the vector to store pointers (with booking*
instead of booking as the data type). Then you can push_back &v, instead of
simply v, in addBooking. You'll also need to modify the code that accesses
the items so that it's using pointers.

Try again, and if you have a specific problem, post the code that you're
having problems with, and tell us exactly what problem you're having. Then
we can suggest a fix.

-Howard
Thanks for the fast reply Howard, heres the bits i've changed:

The push back:
void bookingRecords::addBooking(booking& v)
{
m_bookings.push_back( &v ); //push to back of vector
}

The bookingRecord class:
private :
//vector< booking m_bookings;
//vector that links to booking records
vector< booking* m_bookings;

The iterator bit:
string bookingRecords::toString()
{
string retv("");
vector< booking* >::iterator iter;
for (iter = m_bookings.begin(); iter !=m_bookings.end(); iter++)
{
retv+= iter->BtoString() + "\n";
}

return(retv);
}

Now the error Im getting is that BtoString is undeclared, that is the
line I think im having problems with. Ive tried all sorts of variations
for example &iter- *iter-&iter. *iter. but i cannot find the code
to make it work.

Am i missing something?
Dec 14 '06 #3
Mike Jolley wrote:
Now the error Im getting is that BtoString is undeclared, that is the
line I think im having problems with. Ive tried all sorts of variations
for example &iter- *iter-&iter. *iter. but i cannot find the code
to make it work.

Am i missing something?
Did you try the following?
(*iter)->BtoString()

-binds stronger than *

--
Tobias Gneist
Dec 14 '06 #4
Tobias Gneist wrote:
Mike Jolley wrote:
>Now the error Im getting is that BtoString is undeclared, that is the
line I think im having problems with. Ive tried all sorts of variations
for example &iter- *iter-&iter. *iter. but i cannot find the code
to make it work.

Am i missing something?

Did you try the following?
(*iter)->BtoString()

-binds stronger than *
Cheers that fixed that error, its still not working so Ill spend some
time trying stuff out and if I stay stuck Ill post again later. Thanks
again :)
Dec 14 '06 #5
Right I'm having problem with loading from my file, the problem occurs
in the class method 'load()'

Heres an extract -
if ( boatType[0]=='M' )
{
booking* boatRec = new motorBoat(
customerName,boatName,boatLength,boatDraft,dur,dat e );
tok = strtok( NULL, "," );
motors = atoi(tok);
boatRec->setMotors(motors);
addBooking(boatRec);
counter++;
}

I get errors with both -
boatRec->setMotors(motors);
"class booking has no member named setMotors"
- and -
addBooking(boatRec);
"no matching function for call to `bookingRecords::addBooking(booking*&)"

Which is weird because setMotors DOES exist in the motorBoat sub class
and addBooking DOES exist in the recordBooking class (where this code is
inside).

Any ideas?
Dec 14 '06 #6

"Mike Jolley" <jo**********@tesco.netwrote in message
news:47*****************@newsfe6-win.ntli.net...
Right I'm having problem with loading from my file, the problem occurs in
the class method 'load()'

Heres an extract -
if ( boatType[0]=='M' )
{
booking* boatRec = new motorBoat(
customerName,boatName,boatLength,boatDraft,dur,dat e );
tok = strtok( NULL, "," );
motors = atoi(tok);
boatRec->setMotors(motors);
addBooking(boatRec);
counter++;
}

I get errors with both -
boatRec->setMotors(motors);
"class booking has no member named setMotors"
- and -
addBooking(boatRec);
"no matching function for call to `bookingRecords::addBooking(booking*&)"

Which is weird because setMotors DOES exist in the motorBoat sub class and
addBooking DOES exist in the recordBooking class (where this code is
inside).

Any ideas?
You've left out a lot of details, but I can guess:

The motorBoat class defines the function setMotors(), but it's not defined
in the booking class. You can solve this by adding an abstract virtual
function called setMotors() in the base class. Or, you could cast the
boatRec pointer as a motorBoat*. Or, you could define the boatRec pointer
as a motorBoat* to begin with. (I don't think you'll have to cast it to add
it to the vector, but if the compiler complains, then you can cast it
there.)

Or, since this appears to be initialization code, you could put the
setMotors call inside the constructor of the motorBoat object (and pass the
constructor anything it needs).

Or, you could create a virtual function (Initialize(), or something), which
is overridden in each subclass to do things like that code above.

The problem with the addBooking call is probably that you're passing
boatRec, which is a pointer, to addBooking, which expects a boatRec&. You
can fix that by calling addBooking lke this: addBooking(*boatRec);

-Howard
Dec 14 '06 #7

"Howard" <al*****@hotmail.comwrote in message news:BE_fh.199416
The problem with the addBooking call is probably that you're passing
boatRec, which is a pointer, to addBooking, which expects a boatRec&. You
correction: I meant to say "...expects a motorBoat&".
can fix that by calling addBooking lke this: addBooking(*boatRec);


Dec 14 '06 #8
Howard wrote:
"Mike Jolley" <jo**********@tesco.netwrote in message
news:47*****************@newsfe6-win.ntli.net...
>Right I'm having problem with loading from my file, the problem occurs in
the class method 'load()'

Heres an extract -
if ( boatType[0]=='M' )
{
booking* boatRec = new motorBoat(
customerName,boatName,boatLength,boatDraft,dur,da te );
tok = strtok( NULL, "," );
motors = atoi(tok);
boatRec->setMotors(motors);
addBooking(boatRec);
counter++;
}

I get errors with both -
boatRec->setMotors(motors);
"class booking has no member named setMotors"
- and -
addBooking(boatRec);
"no matching function for call to `bookingRecords::addBooking(booking*&)"

Which is weird because setMotors DOES exist in the motorBoat sub class and
addBooking DOES exist in the recordBooking class (where this code is
inside).

Any ideas?

You've left out a lot of details, but I can guess:

The motorBoat class defines the function setMotors(), but it's not defined
in the booking class. You can solve this by adding an abstract virtual
function called setMotors() in the base class. Or, you could cast the
boatRec pointer as a motorBoat*. Or, you could define the boatRec pointer
as a motorBoat* to begin with. (I don't think you'll have to cast it to add
it to the vector, but if the compiler complains, then you can cast it
there.)

Or, since this appears to be initialization code, you could put the
setMotors call inside the constructor of the motorBoat object (and pass the
constructor anything it needs).

Or, you could create a virtual function (Initialize(), or something), which
is overridden in each subclass to do things like that code above.

The problem with the addBooking call is probably that you're passing
boatRec, which is a pointer, to addBooking, which expects a boatRec&. You
can fix that by calling addBooking lke this: addBooking(*boatRec);

-Howard

Thankyou so much! Hit the nail on the head. I used the motorBoat* and I
used the addBooking(*boatRec).

Thanks a million, Mike.
Dec 14 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Richard B. Kreckel | last post by:
Hi! I was recently asked what book to recommend for a beginner in C++. I am convinced that you needn't study C in depth before learning C++ (though it helps), but cannot find any beginner's...
0
by: Gary Bouchard | last post by:
MYSQL Version 4.0.15 Can some anyone tell me what is wrong with the following statement; select bookings.key1,bookings.surname,bookings.firstname,bookings.bookingseqno,bookings.filetype from...
0
by: Gary Bouchard | last post by:
Can someone please tell me what is wrong with this statement? I have upgraded to MySQL 41. and 5.0 and cannot get MySQL to return the correct results from my database. This same query works...
6
by: Dan Evans | last post by:
Hi, Can anyone help me on a little problem I am having with some SQL - in particular on a subquery. I am setting up a database in Access for a voluntary group which runs
2
by: Tan Tze Yong | last post by:
Dear all, I am tasked to write a program in C++ recently. However my knowledge in C++ is very limited. What happened is I am storing serveral vectors in a map. I need to retrieve the vector from...
8
by: ^MisterJingo^ | last post by:
Hi all, I have a question regarding inheritance. I'll use the following code for an example (its been stripped down to the minimum): // code start using System; class Animal {
4
by: mp | last post by:
I am doing pairwise comparisons between 2 vectors of chars and permuting one vector and storing the resulting calculations in a vector<float> then I find a p-value among other stats. I have to do...
1
by: Geoff | last post by:
I am running an append query using query by example (but included equivalent SQL code). The query counts the number of bookings and appends this number to bookings to Tbl_Weekly INSERT INTO...
8
by: Martin the Third | last post by:
Hi, I need some help! I'm writing an infinite-precision floating point library called ipfloat (I know infinite is a misnomer - but arbitrary was taken). A quick overview: I'm storing numbers as...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.