1

I need to assign set<> iterator to pointer, while I pass set as argument void ProcessVessel(ContainerTerminal* containerTerminal, set<ArrivedVessel> * arrivedVesselPool) whereas ArrivedVessel is a class and after that I made iterator std::set<ArrivedVessel>::iterator it; for loop and I made for loop for (it = arrivedVesselPool->begin(); it != arrivedVesselPool->end(); it++), till now compiler doesn't show me any error but when I assign it to the pointer which is Vessel* currentVessel like this currentVessel = &(*it);, compiler gives error

A value of type 'const ArrivedVessel*' cannot be assigned to an entity of type 'vessel*'.

However for converting iterator I found it on this link iterator to pointer conversion but I couldn't understand it perfectly.

Here is the piece of source code

void ProcessVessel(set<ArrivedVessel> * arrivedVesselPool) {
        std::set<ArrivedVessel>::iterator it;
        for (it = arrivedVesselPool->begin(); it != arrivedVesselPool->end(); it++)
        {
            Vessel* currentVessel;
            currentVessel = &(*it);}

Any suggestions would be very helpful. Thank you in advance.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
ali
  • 13
  • 3

1 Answers1

4

std::set doesn't allow you to modify the objects stored within it so its iterators return const references. You can only therefore assign to a const pointer:

void ProcessVessel(set<ArrivedVessel> * arrivedVesselPool) {
    std::set<ArrivedVessel>::iterator it;
    for (it = arrivedVesselPool->begin(); it != arrivedVesselPool->end(); it++)
    {
        const ArrivedVessel* currentVessel;
        currentVessel = &(*it);
    }

If you want to modify the elements you need to use a different container. For example you could use std::vector, you would have to take care of making sure elements are unique yourself. You'd also need to make sure that the items remain unique after they have been modified.

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
  • 1
    `const Vessel* currentVessel;` should likely be `const ArrivedVessel* currentVessel;` (unless `ArrivedVessel` is inherited from `Vessel`). – Daniel Langr Mar 04 '20 at 08:22
  • Yes, in this way it is working but I want to use `curentVessel` as a non-const pointer so I can pass it to other functions as well. In this way I can not use it to pass to the functions. for example **ct->AssignBerth(*currentVessel)** it shows `qualifiers dropped in binding reference of type "vessel&" to initializer of type "const vessel" ` – ali Mar 04 '20 at 08:38
  • 1
    You can't modify the elements of a `std::set`, if you want to modify the elements use a different container – Alan Birtles Mar 04 '20 at 08:39
  • @AlanBirtles sir could you please give me example, how I can handle this all in same function. – ali Mar 04 '20 at 08:41
  • I've added some more comments. Difficult to provide much more detail without knowing what you are trying to do – Alan Birtles Mar 04 '20 at 08:46
  • You could also extract the `ArrivedVessel` from the `set`, pass it to the functions that needs to modify it and then reinsert it in the `set`. – Ted Lyngmo Mar 04 '20 at 08:48
  • I tried it by using const_cast `step 1 ->` `const Vessel& cv =*it;` and `step 2->` `Vessel& currentVessel = const_cast(cv);` – ali Mar 05 '20 at 05:02
  • @AlanBirtles I tried it by using const_cast `step 1 -> const Vessel& cv =*it;` and `step 2->` `Vessel& currentVessel = const_cast(cv);` – ali Mar 05 '20 at 11:31
  • `const_cast` will fix the compile error but you'll get undefined behaviour if you make modifications to the objects which would change their ordering in the `std::set` – Alan Birtles Mar 05 '20 at 12:01