problem need help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Oliver Bleckmann

    #1

    problem need help

    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->getRoomNumbe r)

    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 // Accessormethode n beginnen mit get,
    dann const, sind virtual, klein
    {
    return _zimmerzahl;
    }
    virtual void setZimmerZahl(i nt zimmer) // Accessormethode n beginnen mit
    set, sind virtual, klein
    {
    _zimmerzahl = zimmer;
    }
    Hotel(std::stri ng 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

    #2
    Re: problem need help

    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->getRoomNumbe r)
    >
    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<Zim merzimmers;
    ...
    };
    [..]
    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Oliver Bleckmann

      #3
      Re: problem need help

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


      Comment

      • chenxiaoxin@yahoo.com

        #4
        Re: problem need help

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

        Comment

        • LR

          #5
          Re: problem need help

          chenxiaoxin@yah oo.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

          Comment

          • Erik Wikström

            #6
            Re: problem need help

            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->getRoomNumbe r)
            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<Zim merzimmers; // vector holding Zimemrs
            Hotel(int nr) { // Create and init Zimmers
            for (int i = 0; i < nr; ++i)
            zimmers.push_ba ck(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<Zim mer*zimmers; // vector holding pointers to Zimmers
            Hotel(int nr) { // Create and init Zimmers
            for (int i = 0; i < nr; ++i)
            zimmers.push_ba ck(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;
            }

            Comment

            • Oliver Bleckmann

              #7
              Re: problem need help

              Thnks Erik i think this will be usefull...


              Comment

              • mlimber

                #8
                Re: problem need help

                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->getRoomNumbe r)
                >
                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<Zim merzimmers; // 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_ba ck(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<Zim mer*zimmers; // vector holding pointers to Zimmers
                Hotel(int nr) { // Create and init Zimmers
                for (int i = 0; i < nr; ++i)
                zimmers.push_ba ck(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::share d_ptr aka
                boost::shared_p tr) rather than a raw pointer.

                Cheers! --M

                Comment

                • mlimber

                  #9
                  Re: problem need help

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



                  Cheers! --M

                  Comment

                  Working...