Printing vector contents through a Class Member Function (RUNTIME ERROR)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shaunb
    New Member
    • Aug 2007
    • 7

    Printing vector contents through a Class Member Function (RUNTIME ERROR)

    Hi.

    I am, hopefully, coming towards the end of my program, just need a little bit of help with this runtime error on my program:

    An access violation (Segmentation Fault) rasied in your program.
    This appears when i run the debugger, appears when i get to the line where i call the printDetails() function:

    Code:
    while (h_bCount != h_rooms)
        {
            hoteldetails >> h_rNumber;
            hoteldetails >> h_rate;
            getline(hoteldetails, h_facilities);
            
            //cout << h_rNumber << h_rate << h_facilities << endl;
            
            Bedroom *nwbed = new Bedroom(h_rNumber, h_rate, h_facilities);
                    
            hotel.push_back(nwbed);
            int n;
            n = 0;
            while (n < hotel.size())
            {
                  hotel[n]->Room::printDetails(); // ERROR APPEARS ON THIS LINE
                  n++;
            }
    This is my class member function for printing the room details:

    Code:
    void Room::printDetails()
    {
         cout << "Bedroom: " << roomNumber << "Rate: " << rate << endl;
    }
    The above is also what is highlighted when i click OK on the error that appears with the debugger.

    Any help would be greatly appreciated!!

    Thanks

    Shaun B
  • RRick
    Recognized Expert Contributor
    • Feb 2007
    • 463

    #2
    Check the value of hotel[n]. I suspect you have a bad pointer value. I'm not sure why you would have this problem, but when working with pointers and getting seg fault errors, this is the most common cause. Did you delete the pointer before? Does the pointer look corrupted?

    Also, check the object values before and after setting the vector. Do they look good or are they corrupted? This might give you an idea of what is happening. Take your time and use your brain; these bugs can be hard to find.

    Good luck.

    Comment

    • Ganon11
      Recognized Expert Specialist
      • Oct 2006
      • 3651

      #3
      Not entirely sure if this will work, but did you try

      [CODE=cpp]hotel[n]->printDetails() ;[/CODE]

      rather than

      [CODE=cpp]hotel[n]->Room::printDet ails();[/CODE]

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You can't have it both ways:

        Originally posted by shaunb
        hotel.push_back (nwbed);
        and later:
        Originally posted by shaunb
        hotel[n]->Room::printDet ails(); // ERROR APPEARS ON THIS LINE
        In the first case hotel is a vector<Room>.

        In the second case hotel[n] is a Room. Not a pointer to a Room.

        Use the member-of operator:
        [code=cpp]
        hotel[n].printDetails() ;
        [/code]

        Comment

        • shaunb
          New Member
          • Aug 2007
          • 7

          #5
          hi, i have tried what people have said, but its doing exactly the same thing. im not sure how to check wether the pointers are corrupt or anything like that.

          the following is the code which i think is relevant:

          These are my classes:

          Code:
          class Room{
                          protected:
                              int roomNumber;
                              double rate;
                              string customer;
                              Date booking;
                          
                          public:
                              Room (int, double);
                              void bookRoom (string, Date);
                              Date getBooking();
                              double getRate();
                              int getRoomNumber();
                              void printDetails();
                                                   
                     };
                     
          class Bedroom: public Room{
                      
                          private:
                              string facilities;
                              
                          public:
                              Bedroom (int, double, string);
                              void printDetails();
                       };
                       
          class ConferenceRoom: public Room{
                     
                          private:
                              int seatingCapacity;
                              
                          public:
                              ConferenceRoom (int, double, int);
                              int getCapacity();
                              void printDetails();
                              };
          This is the printDetails function within the Room class:

          Code:
          void Room::printDetails()
          {
               cout << "Bedroom: " << roomNumber << "Rate: " << rate << endl;
          }
          vector:

          Code:
          vector <Room*> hotel;
          But i need the vector contents to be printed through the Bedroom and the Conference Room classes member functions names printDetails, but i cant even get it to compile when i do this.

          this is my code that puts objects into vectors, i have added a bit which prints out the values before been placed into an object and aftewr they have been put inot an object and assigned to a pointer, but before they are put into the vector.

          The first print out is fine, but when i output the contents of the pointer through the printDetails function, it returns a lot of number.

          Code:
          while (h_bCount != h_rooms)
              {
                  hoteldetails >> h_rNumber;
                  hoteldetails >> h_rate;
                  getline(hoteldetails, h_facilities);
                  
                  cout << h_rNumber << "\t" << h_rate << "\t" << h_facilities << endl;
                  
                  Bedroom *nwbed = new Bedroom(h_rNumber, h_rate, h_facilities);
                  
                  nwbed->printDetails();     
                  hotel.push_back(nwbed);
                            
                  h_bCount++;
              }
          Output before entering pointer:

          201 52.50 single ensuite
          202 85.00 double ensuite balcony
          203 58.50 single ensuite sea-view
          and after:

          Bedroom: 4063776 Rate: 7.03057e+199
          Bedroom: 4064136 Rate: 0
          Bedroom: 0 Rate: 4.71332e+257
          This is the part where i call the function printDetails to output vectors contents:

          Code:
          cout << "All Rooms: \n"<< endl;
                     
                     while (n < hotel.size())
                     {
                          hotel[n]->printDetails();
                          hotel[n]->printDetails();
                      }
          i had to use -> as when i used . it said printDetails has not been declared and i couldnt get it to compile like this!

          I am so confused right now i think my head is going to explode!

          Has anyone got any help with how i could print my vectors contents through the printDetails function!

          Please!!!!

          Shaun B

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by shaunb
            hi, i have tried what people have said, but its doing exactly the same thing. im not sure how to check wether the pointers are corrupt or anything like that.

            the following is the code which i think is relevant:

            These are my classes:

            Code:
            class Room{
                            protected:
                                int roomNumber;
                                double rate;
                                string customer;
                                Date booking;
                            
                            public:
                                Room (int, double);
                                void bookRoom (string, Date);
                                Date getBooking();
                                double getRate();
                                int getRoomNumber();
                                void printDetails();
                                                     
                       };
                       
            class Bedroom: public Room{
                        
                            private:
                                string facilities;
                                
                            public:
                                Bedroom (int, double, string);
                                void printDetails();
                         };
                         
            class ConferenceRoom: public Room{
                       
                            private:
                                int seatingCapacity;
                                
                            public:
                                ConferenceRoom (int, double, int);
                                int getCapacity();
                                void printDetails();
                                };
            This is the printDetails function within the Room class:

            Code:
            void Room::printDetails()
            {
                 cout << "Bedroom: " << roomNumber << "Rate: " << rate << endl;
            }
            vector:

            Code:
            vector <Room*> hotel;
            But i need the vector contents to be printed through the Bedroom and the Conference Room classes member functions names printDetails, but i cant even get it to compile when i do this.

            this is my code that puts objects into vectors, i have added a bit which prints out the values before been placed into an object and aftewr they have been put inot an object and assigned to a pointer, but before they are put into the vector.

            The first print out is fine, but when i output the contents of the pointer through the printDetails function, it returns a lot of number.

            Code:
            while (h_bCount != h_rooms)
                {
                    hoteldetails >> h_rNumber;
                    hoteldetails >> h_rate;
                    getline(hoteldetails, h_facilities);
                    
                    cout << h_rNumber << "\t" << h_rate << "\t" << h_facilities << endl;
                    
                    Bedroom *nwbed = new Bedroom(h_rNumber, h_rate, h_facilities);
                    
                    nwbed->printDetails();     
                    hotel.push_back(nwbed);
                              
                    h_bCount++;
                }
            Output before entering pointer:



            and after:

            i had to use -> as when i used . it said printDetails has not been declared and i couldnt get it to compile like this!

            I am so confused right now i think my head is going to explode!

            Has anyone got any help with how i could print my vectors contents through the printDetails function!

            Please!!!!

            Shaun B
            If you make the printDetails function virtual you could use that to call the bedroom and conference room printDetails instead of the base class Room's printDetails.
            Are you still getting that seg fault?

            P.S. What does your constructor look like for the room and bedroom classes?

            Comment

            • shaunb
              New Member
              • Aug 2007
              • 7

              #7
              hi, i have made all the printDetails functions virtual, how do i call the different ones for the different type objects??

              yes, i am still getting the seg fault, dont have a clue what is causing it.

              Here are my constructors:

              Room:

              Code:
              Room::Room (int, double)
              {
                  int roomNumber = 0;
                  double rate = 0;
              }
              Bedroom:

              Code:
              Bedroom::Bedroom(int roomNumber, double rate, string facilities): Room(roomNumber, rate)
              {    
                  roomNumber = 0;
                  rate = 0.0;
                  facilities = " ";
              }
              ConferenceRoom:

              Code:
              ConferenceRoom::ConferenceRoom(int roomNumber, double rate, int seatingCapacity): Room(roomNumber, rate)
              {
                  roomNumber = 0;
                  rate = 0.0;
                  seatingCapacity = 0;                                   
              }
              Any ideas?

              Shaun B

              Comment

              • ilikepython
                Recognized Expert Contributor
                • Feb 2007
                • 844

                #8
                Originally posted by shaunb
                hi, i have made all the printDetails functions virtual, how do i call the different ones for the different type objects??
                Let me give you an example of virtual functions:
                [code=cpp]
                class Room
                {
                protected:
                int roomNumber;
                public:
                Room::Room(int r): roomNumber(r){}
                void printDetails() { cout << "Room Number: " << roomNumber << endl; };
                };
                class BedRoom: public Room
                {
                public:
                BedRoom::BedRoo m(int r): Room(r){};
                void printDetails() { cout << "BedRoom Number: " << roomNumber << endl; };
                };
                class ConferenceRoom: public Room
                {
                public:
                ConferenceRoom: :ConferenceRoom (int r): Room(r){};
                void printDetails() { cout << "Conference Room Number: " << roomNumber << endl; };
                };


                int main()
                {
                Room *rooms[3];

                Room *mypRoom = new Room(123);
                Room *mypBedRoom = new BedRoom(245);
                Room *mypConferenceR oom = new ConferenceRoom( 836);

                rooms[0] = mypRoom;
                rooms[1] = mypBedRoom;
                rooms[2] = mypConferenceRo om;

                for (int x = 0; x < 3; x++)
                rooms[x]->printDetails() ;

                cin.get();
                return 0;
                }
                [/code]
                Notice that there are no virtual functions. If you run the program, you'll realize that only Room::printDeta ils is being called even thought some of the pointers are pointing to a bedroom and conference room. If you declare the printDetails function virtual in the base class like this:
                [code=cpp]
                virtual void printDetails();
                [/code]
                The output will be different and the correct function will be called for the correct pointer.
                Originally posted by shaunb
                yes, i am still getting the seg fault, dont have a clue what is causing it.

                Here are my constructors:

                Room:

                Code:
                Room::Room (int, double)
                {
                    int roomNumber = 0;
                    double rate = 0;
                }
                Bedroom:

                Code:
                Bedroom::Bedroom(int roomNumber, double rate, string facilities): Room(roomNumber, rate)
                {    
                    roomNumber = 0;
                    rate = 0.0;
                    facilities = " ";
                }
                ConferenceRoom:

                Code:
                ConferenceRoom::ConferenceRoom(int roomNumber, double rate, int seatingCapacity): Room(roomNumber, rate)
                {
                    roomNumber = 0;
                    rate = 0.0;
                    seatingCapacity = 0;                                   
                }
                Any ideas?

                Shaun B
                Try doing something like this:
                [code=cpp]
                vector <Room *> rooms;

                for (int x = 0; x < 10; x++) // whichever way you want to insert into the vector
                {
                Room *pbedroom = new BedRoom(245 + x, 8.0, "blah blah blah");
                rooms.push_back (pbedroom)
                }

                for (int x = 0; x < rooms.size(); x++)
                {
                rooms[x]->printDetails() ;
                }
                [/code]
                If that doesn't work, try using a regular array.

                Comment

                Working...