0

I am making a set of objects, and I am modifying a vector of strings at a specific index (the vector is a member of a class), but an error appears. What is the problem?

void seat(set<bus> &b1, int &bno, int &seatno)
{
    string name;
    cout << "Enter passenger's name: ";
    cin >> name;

    for(auto &c: b1)
    {
        if (c.bus_no == bno)
        {
            c.vec.at(seatno-1) = name;
        }

        break;
    }
}

What does this error mean?

||=== Build: Debug in bus reservation (compiler: GNU GCC Compiler) ===|
C:\Users\metr\Desktop\bus reservation\main.cpp||In function 'void seat(std::set<bus>&, int&, int&)':|
C:\Users\metr\Desktop\bus reservation\main.cpp|38|error: passing 'const value_type {aka const std::__cxx11::basic_string<char>}' as 'this' argument discards qualifiers [-fpermissive]|
C:\Program Files (x86)\CodeBlocks\MinGW\lib\gcc\mingw32\5.1.0\include\c++\bits\basic_string.h|548|note:   in call to 'std::__cxx11::basic_string<_CharT, _Traits, _Alloc>& std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::operator=(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]'|
||=== Build failed: 1 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user1
  • 21
  • 5
  • 3
    `c` is a reference to a `const bus`, so the `const` version of `at` is used and the value in the `vector` cannot be changed. See this for why `bus` is `const`: [Why does std::set seem to force the use of a const_iterator?](https://stackoverflow.com/questions/38768432/why-does-stdset-seem-to-force-the-use-of-a-const-iterator) – user4581301 Aug 27 '19 at 23:39
  • 2
    In other words, you cannot just reach into a `std::set` and modify an element of that set. – AnT stands with Russia Aug 27 '19 at 23:53
  • 1
    Well, if the set contains a class, a member of the class can be made mutable and thusly modifiable; however if the modification changes the modified value's strict weak ordering, this will result in undefined behavior. If you understand what this means, you should understand enough to do this correctly. If you don't fully understand what all of this means, then don't do it. – Sam Varshavchik Aug 28 '19 at 00:08
  • 1
    The underlying data structure of the set is built on the original values used in the set. Mess around with the original value in the set will corrupt the data structure, i.e. the values will not represent what the structure is supposed to represent. If it's a red-black tree, the red and black nodes may be all out of whack with the data they represent. In short, you have to pluck out the value you want to change out of the set, change it independently, and plop the new value back into the set so that the set's data structure can "rebuild" itself with the new value. – PaulMcKenzie Aug 28 '19 at 00:13
  • I've never tried removing from and inserting into a `set` while in a range-based for loop. In general it gets quite messy, so I recommend a bit of experimentation to see just how messy with a smaller, easy-to-debug test case before trying it here. – user4581301 Aug 28 '19 at 00:23
  • thank you all very much this solved my problem and helped me so much, thanks alot. – user1 Aug 28 '19 at 00:31
  • I made the vector mutable and it worked perfect the vector was modified with the first object but when creating a second object the vector of second object was not modified what is the problem? – user1 Aug 28 '19 at 02:18
  • What is the purpose of a loop with only one iteration (due to the `break`)? – Peter Mortensen Sep 18 '19 at 00:43

0 Answers0