Connecting Tech Pros Worldwide Help | Site Map

problem need help

Oliver Bleckmann
Guest
 
Posts: n/a
#1: Nov 15 '06
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();
}
}
};


Victor Bazarov
Guest
 
Posts: n/a
#2: Nov 15 '06

re: problem need help


Oliver Bleckmann wrote:
Quote:
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;
...
};
Quote:
[..]
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Oliver Bleckmann
Guest
 
Posts: n/a
#3: Nov 15 '06

re: problem need help


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


chenxiaoxin@yahoo.com
Guest
 
Posts: n/a
#4: Nov 15 '06

re: problem need help


Quote:
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));

LR
Guest
 
Posts: n/a
#5: Nov 15 '06

re: problem need help


chenxiaoxin@yahoo.com wrote:
Quote:
Quote:
> 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
Erik Wikström
Guest
 
Posts: n/a
#6: Nov 15 '06

re: problem need help


On 2006-11-15 15:19, Oliver Bleckmann wrote:
Quote:
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;
}
Oliver Bleckmann
Guest
 
Posts: n/a
#7: Nov 15 '06

re: problem need help


Thnks Erik i think this will be usefull...


mlimber
Guest
 
Posts: n/a
#8: Nov 15 '06

re: problem need help


Erik Wikström wrote:
Quote:
On 2006-11-15 15:19, Oliver Bleckmann wrote:
Quote:
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).
Quote:
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

mlimber
Guest
 
Posts: n/a
#9: Nov 15 '06

re: problem need help


mlimber wrote:
Quote:
This is not exception-safe.
Compare:

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

Cheers! --M

Closed Thread