-1

I am a c++ beginner. I was practicing how to work with class and header files. The program manages Rents of a building. It has a Rent class and a Floor class. Here're some definitions of those classes.

rent.cpp (Copied important functions only)


Manages information about Floors and has vector<Floor> paid_by_floors; property.

#include "rent.hh"

void Rent::set_floor_count(int count){
    this->paid_per_floors.reserve(count);
    this->paid_per_floors.resize(count);
}

void Rent::set_floor(int index, Floor floor){
    this->paid_per_floors[index] = floor;
}

Floor Rent::get_floor(int index){
    return this->paid_per_floors[index];
}

floor.cpp (Copied important functions only)


Manages information about rooms and has vector<Floor> paid_by_rooms; property.

#include "floor.hh"

void Floor::set_room_count(int count){
    this->paid_per_rooms.resize(count, 0);
}

int Floor::get_room_amount(int index){
    return this->paid_per_rooms[index];
}

void Floor::set_room_amount(int index, int amount){
    this->paid_per_rooms.push_back(amount);
    // Tried with paid_per_rooms[index] = amount;
}

main.cpp

int main(int argc, char const *argv[])
{

    Rent m_rent(5);
    cout << m_rent.get_floor_count() << endl; // stdout line:1

    m_rent.set_floor_count(2);
    cout << m_rent.get_floor_count() << endl; // stdout line:2 (correct)

    m_rent.get_floor(0).set_room_count(5);
    cout << m_rent.get_floor(0).get_room_count() << endl; // stdout lines:3 (incorrect)

    m_rent.get_floor(0).set_room_amount(0, 100);
    m_rent.get_floor(0).get_room_amount(0); // stdout line:4 (seg fault)

    return 0;
}

And output on stdout.

5
2
0
Segmentation fault (core dumped)

As you can see When I change the size of the paid_per_floors vector. It works. But, it does not work with paid_per_rooms. Also, it returns a segmentation fault which stands for OutOfBound. How should I fix it?

Did Some Research Before

Searched on google about this problem with queries like vector resize does not work, vector resize does not increase size. The answers were to use push_back without array indexing like vec[0]. In set_room_count function I have also used reserve before resize. But, the problem didn't get fixed.

  • 3
    This question would benefit greatly from a [mre], rather than lots of code with missing parts. – Drew Dormann Aug 31 '21 at 16:53
  • 2
    `Floor Rent::get_floor(int index);` As a beginner you should start with reading a [book on C++ basics](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). That book will explain what the difference between returning a value (`Floor`) and a reference (`Floor&`) is. – Evg Aug 31 '21 at 16:53
  • 1
    Welcome to SO. First thing to find out where your understanding differs from how it actually works is to come up with an example that does reproduce the problem. And *only* the problem. Such an example should not be about floors and rooms, but just about a vector and its size, self-cobtained and compilable. You will find that coming up with such an example will solve the problem most of the times, as the error becomes apparent. – DevSolar Aug 31 '21 at 16:54
  • `Copied important functions only`: If it is broken how do you know you have copied the important functions! I don't see `get_floor()` which seems rather important. – Martin York Aug 31 '21 at 17:41
  • @MartinYork I'm sorry. I missed it while editing to decrease codes. – Ashraful Alam Shakil Aug 31 '21 at 17:46

1 Answers1

0

The

Floor Rent::get_floor(int index) {
    return this->paid_per_floors[index];
}

returns a copy of the Floor object and you apply the reservation to the copy while the original object is still unchanged.

You should return a reference so that you will change the original object:

Floor& Rent::get_floor(int index) {
    return this->paid_per_floors[index];
}
Damir Tenishev
  • 1,275
  • 3
  • 14