473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

7
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:

Expand|Select|Wrap|Line Numbers
  1. while (h_bCount != h_rooms)
  2.     {
  3.         hoteldetails >> h_rNumber;
  4.         hoteldetails >> h_rate;
  5.         getline(hoteldetails, h_facilities);
  6.  
  7.         //cout << h_rNumber << h_rate << h_facilities << endl;
  8.  
  9.         Bedroom *nwbed = new Bedroom(h_rNumber, h_rate, h_facilities);
  10.  
  11.         hotel.push_back(nwbed);
  12.         int n;
  13.         n = 0;
  14.         while (n < hotel.size())
  15.         {
  16.               hotel[n]->Room::printDetails(); // ERROR APPEARS ON THIS LINE
  17.               n++;
  18.         }
This is my class member function for printing the room details:

Expand|Select|Wrap|Line Numbers
  1. void Room::printDetails()
  2. {
  3.      cout << "Bedroom: " << roomNumber << "Rate: " << rate << endl;
  4. }
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
Aug 18 '07 #1
7 1493
RRick
463 Expert 256MB
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.
Aug 18 '07 #2
Ganon11
3,652 Expert 2GB
Not entirely sure if this will work, but did you try

Expand|Select|Wrap|Line Numbers
  1. hotel[n]->printDetails();
rather than

Expand|Select|Wrap|Line Numbers
  1. hotel[n]->Room::printDetails();
Aug 18 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
You can't have it both ways:

hotel.push_back(nwbed);
and later:
hotel[n]->Room::printDetails(); // 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:
Expand|Select|Wrap|Line Numbers
  1.  hotel[n].printDetails(); 
  2.  
Aug 18 '07 #4
shaunb
7
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:

Expand|Select|Wrap|Line Numbers
  1. class Room{
  2.                 protected:
  3.                     int roomNumber;
  4.                     double rate;
  5.                     string customer;
  6.                     Date booking;
  7.  
  8.                 public:
  9.                     Room (int, double);
  10.                     void bookRoom (string, Date);
  11.                     Date getBooking();
  12.                     double getRate();
  13.                     int getRoomNumber();
  14.                     void printDetails();
  15.  
  16.            };
  17.  
  18. class Bedroom: public Room{
  19.  
  20.                 private:
  21.                     string facilities;
  22.  
  23.                 public:
  24.                     Bedroom (int, double, string);
  25.                     void printDetails();
  26.              };
  27.  
  28. class ConferenceRoom: public Room{
  29.  
  30.                 private:
  31.                     int seatingCapacity;
  32.  
  33.                 public:
  34.                     ConferenceRoom (int, double, int);
  35.                     int getCapacity();
  36.                     void printDetails();
  37.                     };
This is the printDetails function within the Room class:

Expand|Select|Wrap|Line Numbers
  1. void Room::printDetails()
  2. {
  3.      cout << "Bedroom: " << roomNumber << "Rate: " << rate << endl;
  4. }
vector:

Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. while (h_bCount != h_rooms)
  2.     {
  3.         hoteldetails >> h_rNumber;
  4.         hoteldetails >> h_rate;
  5.         getline(hoteldetails, h_facilities);
  6.  
  7.         cout << h_rNumber << "\t" << h_rate << "\t" << h_facilities << endl;
  8.  
  9.         Bedroom *nwbed = new Bedroom(h_rNumber, h_rate, h_facilities);
  10.  
  11.         nwbed->printDetails();     
  12.         hotel.push_back(nwbed);
  13.  
  14.         h_bCount++;
  15.     }
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:

Expand|Select|Wrap|Line Numbers
  1. cout << "All Rooms: \n"<< endl;
  2.  
  3.            while (n < hotel.size())
  4.            {
  5.                 hotel[n]->printDetails();
  6.                 hotel[n]->printDetails();
  7.             }
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
Aug 18 '07 #5
ilikepython
844 Expert 512MB
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:

Expand|Select|Wrap|Line Numbers
  1. class Room{
  2.                 protected:
  3.                     int roomNumber;
  4.                     double rate;
  5.                     string customer;
  6.                     Date booking;
  7.  
  8.                 public:
  9.                     Room (int, double);
  10.                     void bookRoom (string, Date);
  11.                     Date getBooking();
  12.                     double getRate();
  13.                     int getRoomNumber();
  14.                     void printDetails();
  15.  
  16.            };
  17.  
  18. class Bedroom: public Room{
  19.  
  20.                 private:
  21.                     string facilities;
  22.  
  23.                 public:
  24.                     Bedroom (int, double, string);
  25.                     void printDetails();
  26.              };
  27.  
  28. class ConferenceRoom: public Room{
  29.  
  30.                 private:
  31.                     int seatingCapacity;
  32.  
  33.                 public:
  34.                     ConferenceRoom (int, double, int);
  35.                     int getCapacity();
  36.                     void printDetails();
  37.                     };
This is the printDetails function within the Room class:

Expand|Select|Wrap|Line Numbers
  1. void Room::printDetails()
  2. {
  3.      cout << "Bedroom: " << roomNumber << "Rate: " << rate << endl;
  4. }
vector:

Expand|Select|Wrap|Line Numbers
  1. 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.

Expand|Select|Wrap|Line Numbers
  1. while (h_bCount != h_rooms)
  2.     {
  3.         hoteldetails >> h_rNumber;
  4.         hoteldetails >> h_rate;
  5.         getline(hoteldetails, h_facilities);
  6.  
  7.         cout << h_rNumber << "\t" << h_rate << "\t" << h_facilities << endl;
  8.  
  9.         Bedroom *nwbed = new Bedroom(h_rNumber, h_rate, h_facilities);
  10.  
  11.         nwbed->printDetails();     
  12.         hotel.push_back(nwbed);
  13.  
  14.         h_bCount++;
  15.     }
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?
Aug 19 '07 #6
shaunb
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:

Expand|Select|Wrap|Line Numbers
  1. Room::Room (int, double)
  2. {
  3.     int roomNumber = 0;
  4.     double rate = 0;
  5. }
Bedroom:

Expand|Select|Wrap|Line Numbers
  1. Bedroom::Bedroom(int roomNumber, double rate, string facilities): Room(roomNumber, rate)
  2. {    
  3.     roomNumber = 0;
  4.     rate = 0.0;
  5.     facilities = " ";
  6. }
ConferenceRoom:

Expand|Select|Wrap|Line Numbers
  1. ConferenceRoom::ConferenceRoom(int roomNumber, double rate, int seatingCapacity): Room(roomNumber, rate)
  2. {
  3.     roomNumber = 0;
  4.     rate = 0.0;
  5.     seatingCapacity = 0;                                   
  6. }
Any ideas?

Shaun B
Aug 19 '07 #7
ilikepython
844 Expert 512MB
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:
Expand|Select|Wrap|Line Numbers
  1. class Room
  2. {
  3.     protected:
  4.         int roomNumber;
  5.     public:
  6.         Room::Room(int r): roomNumber(r){}
  7.         void printDetails() { cout << "Room Number: " << roomNumber << endl; };
  8. };
  9. class BedRoom: public Room
  10. {
  11.     public:
  12.         BedRoom::BedRoom(int r): Room(r){};
  13.         void printDetails() { cout << "BedRoom Number: " << roomNumber << endl; };
  14. };
  15. class ConferenceRoom: public Room
  16. {
  17.     public:
  18.         ConferenceRoom::ConferenceRoom(int r): Room(r){};
  19.         void printDetails() { cout << "ConferenceRoom Number: " << roomNumber << endl; };
  20. };
  21.  
  22.  
  23. int main()
  24. {
  25.     Room *rooms[3];
  26.  
  27.     Room *mypRoom = new Room(123);
  28.     Room *mypBedRoom = new BedRoom(245);
  29.     Room *mypConferenceRoom = new ConferenceRoom(836);
  30.  
  31.     rooms[0] = mypRoom;
  32.     rooms[1] = mypBedRoom;
  33.     rooms[2] = mypConferenceRoom;
  34.  
  35.     for (int x = 0; x < 3; x++)
  36.         rooms[x]->printDetails();
  37.  
  38.     cin.get();
  39.     return 0;
  40. }
  41.  
Notice that there are no virtual functions. If you run the program, you'll realize that only Room::printDetails 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:
Expand|Select|Wrap|Line Numbers
  1. virtual void printDetails();
  2.  
The output will be different and the correct function will be called for the correct pointer.
yes, i am still getting the seg fault, dont have a clue what is causing it.

Here are my constructors:

Room:

Expand|Select|Wrap|Line Numbers
  1. Room::Room (int, double)
  2. {
  3.     int roomNumber = 0;
  4.     double rate = 0;
  5. }
Bedroom:

Expand|Select|Wrap|Line Numbers
  1. Bedroom::Bedroom(int roomNumber, double rate, string facilities): Room(roomNumber, rate)
  2. {    
  3.     roomNumber = 0;
  4.     rate = 0.0;
  5.     facilities = " ";
  6. }
ConferenceRoom:

Expand|Select|Wrap|Line Numbers
  1. ConferenceRoom::ConferenceRoom(int roomNumber, double rate, int seatingCapacity): Room(roomNumber, rate)
  2. {
  3.     roomNumber = 0;
  4.     rate = 0.0;
  5.     seatingCapacity = 0;                                   
  6. }
Any ideas?

Shaun B
Try doing something like this:
Expand|Select|Wrap|Line Numbers
  1. vector <Room *> rooms;
  2.  
  3. for (int x = 0; x < 10; x++)  // whichever way you want to insert into the vector
  4. {
  5.     Room *pbedroom = new BedRoom(245 + x, 8.0, "blah blah blah");
  6.     rooms.push_back(pbedroom)
  7. }
  8.  
  9. for (int x = 0; x < rooms.size(); x++)
  10. {
  11.     rooms[x]->printDetails();
  12. }
  13.  
If that doesn't work, try using a regular array.
Aug 19 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Steve Johnson | last post by:
Been banging my head on this for two days now. Hope someone can help! My test program below is in the form of a single JSP, with a Node class build in. (All the coded needed to run is below.) ...
7
by: Forecast | last post by:
I run the following code in UNIX compiled by g++ 3.3.2 successfully. : // proj2.cc: returns a dynamic vector and prints out at main~~ : // : #include <iostream> : #include <vector> : : using...
6
by: Dave Reid | last post by:
Hi everyone... I'm pretty much a newbie C++ user, and I've run into a problem. I'm trying to read in a large text file, and then do manipulations on it. I can read it into a large 2-dimensional...
4
by: cpisz | last post by:
At least that is what I think I want to do. What is the proper way for me to return multiple data member objects from an accessor method in my class while ensuring the data does not get changed,...
14
by: Michael Sgier | last post by:
Hello If someone could explain the code below to me would be great. // return angle between two vectors const float inline Angle(const CVector& normal) const { return acosf(*this % normal); }...
8
by: Manuel | last post by:
Hi! If I've a vector filled with abstract classes, can I push in it the derived classes too? Even if derived classes have new methods? I've done some experiments, and it seem I can push the...
11
by: Brian | last post by:
Dear Programmers, I have a class with a pointer to an array. In the destructor, I just freed this pointer. A problem happens if I define a reference to a vector of this kind of class. The...
22
by: sandy | last post by:
I am trying to make a simulated directory structure application for a course. I am using a Vector to store pointers to my Directory objects (as subdirectories of the current object). In my...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.