By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,599 Members | 1,304 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,599 IT Pros & Developers. It's quick & easy.

problem need help

P: n/a
i want to create a object of class Hotel, which own a variable number of
objects
of the class Zimmer (~ rooms). accessed in main like this
Hotel hotel("name",9);
std::cout << hotel.zimmers[1]->getZimmerZahl(); //(~
hotel.rooms->getRoomNumber)

well, i don't know where to put the pointer declaration.
Zimmer* zimmers[_zimmerzahl]; // Array als Pointer auf Objekte
in the constructor
i get "'class Hotel' has no member named 'zimmers' "
putting this line
Zimmer* zimmers[getZimmerZahl]; // Array als Pointer auf Objekte
after the getZimmerZahl (~gerRoomNumber) function leaves _zimmerzahl
uninitialized
error: "`zimmers' has non-integral type `<unknown type>'"

how do can i manage that?

thanks!

here is the code:

class Hotel
{
private:
std::string _hotelname; // Attribute immer private, beginnen mit _, klein
int _zimmerzahl;
public:
virtual int getZimmerZahl() const // Accessormethoden beginnen mit get,
dann const, sind virtual, klein
{
return _zimmerzahl;
}
virtual void setZimmerZahl(int zimmer) // Accessormethoden beginnen mit
set, sind virtual, klein
{
_zimmerzahl = zimmer;
}
Hotel(std::string name, int zimmer) // Konstruktor, ist immer zu
implementieren
{
_hotelname = name; // Defaultwerte
_zimmerzahl = zimmer;
Zimmer* zimmers[_zimmerzahl]; // Array als Pointer auf Objekte
for(int i = 0; i <= _zimmerzahl; i++)
{
zimmers[i] = new Zimmer();
}
}
};
Nov 15 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Oliver Bleckmann wrote:
i want to create a object of class Hotel, which own a variable number
of objects
of the class Zimmer (~ rooms). accessed in main like this
Hotel hotel("name",9);
std::cout << hotel.zimmers[1]->getZimmerZahl(); //(~
hotel.rooms->getRoomNumber)

well, i don't know where to put the pointer declaration.
Zimmer* zimmers[_zimmerzahl]; // Array als Pointer auf Objekte
in the constructor
i get "'class Hotel' has no member named 'zimmers' "
putting this line
Zimmer* zimmers[getZimmerZahl]; // Array als Pointer auf Objekte
after the getZimmerZahl (~gerRoomNumber) function leaves _zimmerzahl
uninitialized
error: "`zimmers' has non-integral type `<unknown type>'"

how do can i manage that?
You need to pick up standard containers, they will help you a lot.

class Hotel
{
std::vector<Zimmerzimmers;
...
};
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 15 '06 #2

P: n/a
You need to pick up standard containers, they will help you a lot.
>
class Hotel
{
std::vector<Zimmerzimmers;
...
};
So whtat does that do? I don't understand...
Nov 15 '06 #3

P: n/a
Zimmer* zimmers[_zimmerzahl]; // Array als Pointer auf Objekte
You can't declare an array on the fly. Use this instead or just use
vector:

Zimmer* zimmers = ( Zimmer*)malloc(_zimmerzahl * sizeof(Zimmer));

Nov 15 '06 #4

P: n/a
LR
ch*********@yahoo.com wrote:
> Zimmer* zimmers[_zimmerzahl]; // Array als Pointer auf Objekte


You can't declare an array on the fly. Use this instead or just use
vector:

Zimmer* zimmers = ( Zimmer*)malloc(_zimmerzahl * sizeof(Zimmer));
Why would you prefer malloc to new?

Why would you do this instead of a std::vector?

How are you going to initialize all of those Zimmer_s?

LR
Nov 15 '06 #5

P: n/a
On 2006-11-15 15:19, Oliver Bleckmann wrote:
i want to create a object of class Hotel, which own a variable number of
objects
of the class Zimmer (~ rooms). accessed in main like this
Hotel hotel("name",9);
std::cout << hotel.zimmers[1]->getZimmerZahl(); //(~
hotel.rooms->getRoomNumber)
You need to make the Zimmer and the Hotel two separate objects, and then
make the Hotel contain a number of Zimmers, each of which has a zhal.
The best way to contain the Zimmers is to place them in an vector in Hotel:

#include <vector>
#include <iostream>

class Zimmer {
private:
int zahl;
public:
Zimmer(int z) : zahl(z) {}
int getZimmerZahl() const {
return zahl;
}
};

class Hotel {
public:
std::vector<Zimmerzimmers; // vector holding Zimemrs
Hotel(int nr) { // Create and init Zimmers
for (int i = 0; i < nr; ++i)
zimmers.push_back(Zimmer(i));
}
};

int main() {
Hotel hotel(5);
std::cout << hotel.zimmers[3].getZimmerZhal();
return 0;
}

It does not use the -operator to dereference a pointer but in most
cases this is better. If you really want to use pointers instead keep
the Zimmer as it is but use the following for Hotel:

class Hotel {
public:
std::vector<Zimmer*zimmers; // vector holding pointers to Zimmers
Hotel(int nr) { // Create and init Zimmers
for (int i = 0; i < nr; ++i)
zimmers.push_back(new Zimmer(i)); // use new
}
~Hotel() { // destructor, needed to clean up
for (int i = 0; i < zimmers.size(); ++i)
delete zimmers[i];

};

Then you need to use the -operator to access the Zimmer-members like
getZimmerZahl():

int main() {
Hotel hotel(5);
std::cout << hotel.zimmers[3]->getZimmerZhal();
return 0;
}
Nov 15 '06 #6

P: n/a
Thnks Erik i think this will be usefull...
Nov 15 '06 #7

P: n/a
Erik Wikström wrote:
On 2006-11-15 15:19, Oliver Bleckmann wrote:
i want to create a object of class Hotel, which own a variable number of
objects
of the class Zimmer (~ rooms). accessed in main like this
Hotel hotel("name",9);
std::cout << hotel.zimmers[1]->getZimmerZahl(); //(~
hotel.rooms->getRoomNumber)

You need to make the Zimmer and the Hotel two separate objects, and then
make the Hotel contain a number of Zimmers, each of which has a zhal.
The best way to contain the Zimmers is to place them in an vector in Hotel:

#include <vector>
#include <iostream>

class Zimmer {
private:
int zahl;
public:
Zimmer(int z) : zahl(z) {}
int getZimmerZahl() const {
return zahl;
}
};

class Hotel {
public:
std::vector<Zimmerzimmers; // vector holding Zimemrs
Public data is generally frowned upon. Encapsulation and data hiding
are your friends (see
http://www.parashift.com/c++-faq-lit....html#faq-7.4).
Hotel(int nr) { // Create and init Zimmers
for (int i = 0; i < nr; ++i)
zimmers.push_back(Zimmer(i));
}
};

int main() {
Hotel hotel(5);
std::cout << hotel.zimmers[3].getZimmerZhal();
return 0;
}

It does not use the -operator to dereference a pointer but in most
cases this is better. If you really want to use pointers instead keep
the Zimmer as it is but use the following for Hotel:

class Hotel {
public:
std::vector<Zimmer*zimmers; // vector holding pointers to Zimmers
Hotel(int nr) { // Create and init Zimmers
for (int i = 0; i < nr; ++i)
zimmers.push_back(new Zimmer(i)); // use new
}
~Hotel() { // destructor, needed to clean up
for (int i = 0; i < zimmers.size(); ++i)
delete zimmers[i];

};
This is not exception-safe. Say that the third allocation of a Zimmer
in the constructor fails throwing an exception (e.g., std::bad_alloc or
something else that Zimmer throws), then the destructor won't be called
and the first two Zimmers (and any additional resources they allocate)
will be leaked. The first approach above is exception-safe, but if you
need pointers, use a smart pointer (e.g., std::tr1::shared_ptr aka
boost::shared_ptr) rather than a raw pointer.

Cheers! --M

Nov 15 '06 #8

P: n/a
mlimber wrote:
This is not exception-safe.
Compare:

http://www.artima.com/cppsource/bigtwo.html

Cheers! --M

Nov 15 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.