problem need help

Discussion in 'C++' started by Oliver Bleckmann, Nov 15, 2006.

  1. 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 = new Zimmer();
    }
    }
    };
     
    Oliver Bleckmann, Nov 15, 2006
    #1
    1. Advertising

  2. 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<Zimmer> zimmers;
    ...
    };

    > [..]


    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 15, 2006
    #2
    1. Advertising

  3. > You need to pick up standard containers, they will help you a lot.
    >
    > class Hotel
    > {
    > std::vector<Zimmer> zimmers;
    > ...
    > };


    So whtat does that do? I don't understand...
     
    Oliver Bleckmann, Nov 15, 2006
    #3
  4. Oliver Bleckmann

    Guest


    > 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, 2006
    #4
  5. Oliver Bleckmann

    LR Guest

    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
     
    LR, Nov 15, 2006
    #5
  6. 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<Zimmer> zimmers; // 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;

    };

    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;
    }
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Nov 15, 2006
    #6
  7. Thnks Erik i think this will be usefull...
     
    Oliver Bleckmann, Nov 15, 2006
    #7
  8. Oliver Bleckmann

    mlimber Guest

    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<Zimmer> zimmers; // vector holding Zimemrs


    Public data is generally frowned upon. Encapsulation and data hiding
    are your friends (see
    http://www.parashift.com/c -faq-lite/classes-and-objects.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;
    >
    > };


    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, Nov 15, 2006
    #8
  9. Oliver Bleckmann

    mlimber Guest

    mlimber, Nov 15, 2006
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. TN Bella
    Replies:
    1
    Views:
    2,506
    Edward
    Jun 18, 2004
  2. Teep
    Replies:
    2
    Views:
    787
    Larry Marburger
    Jun 21, 2004
  3. =?Utf-8?B?Q2hyaXM=?=

    Help Help. I really need some help with this

    =?Utf-8?B?Q2hyaXM=?=, Jan 31, 2007, in forum: ASP .Net
    Replies:
    3
    Views:
    601
    =?Utf-8?B?SmFzb24gVmVybWlsbGlvbg==?=
    Jan 31, 2007
  4. ElementX
    Replies:
    9
    Views:
    535
    RedGrittyBrick
    Oct 1, 2008
  5. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    581
    Bullschmidt
    May 11, 2004
Loading...

Share This Page