Page 1 of 1

[solved] C++ question / PQ::Element suddenly has no name

Posted: Tue May 30, 2017 7:59 am
by Ophiuchus
I'm working on the imperial stockpile and encounter a C++ behaviour I do not understand.

I have a for loop going through all queue elements looking for imperial
projects (i.e. projects which are allowed to use imperial stockpile)

In the loop the element has the ship design name.
As soon as I leave the loop, the ship design name is not there anymore.

From the log you can see that the name: Comsat is gone in update 4 (which is directly after leaving the scope of the for-loop:

Code: Select all

ProductionQueue::Update: 3      seems imperial project was saved: ProductionQueue::Element (ProductionItem: BT_SHIP name: Comsatid: 1) (1) x1  (remaining: 1)
ProductionQueue::Update: 3 last seems imperial project was saved: ProductionQueue::Element (ProductionItem: BT_SHIP name: Comsatid: 1) (1) x1  (remaining: 1)
ProductionQueue::Update: 4      seems imperial project was saved: ProductionQueue::Element (ProductionItem: BT_SHIP id: 1) (1) x1 (remaining: 1)
ProductionQueue::Update: 4 last seems imperial project was saved: ProductionQueue::Element (ProductionItem: BT_SHIP id: 1) (1) x1 (remaining: 1)

the relevant code part:

Code: Select all

// type of QueueType is std::deque<ProductionQueue::Element>
QueueType                   dpsim_queue = sim_queue;
...

std::vector<std::reference_wrapper<ProductionQueue::Element>> imperial_projects;
...

Element* last_one;
for (auto queue_element : dpsim_queue) {
    if (queue_element.allowed_imperial_stockpile_use) {
        imperial_projects.push_back(std::ref(queue_element));
    }
    last_one = &queue_element;
    if (imperial_projects.size() > 0)    DebugLogger() << "ProductionQueue::Update: 3      seems imperial project was saved: " << imperial_projects.back().get().Dump();
    if (last_one)    DebugLogger() << "ProductionQueue::Update: 3 last seems imperial project was saved: " << (*last_one).Dump();
}
if (imperial_projects.size() > 0) DebugLogger() << "ProductionQueue::Update: 4      seems imperial project was saved: " << imperial_projects.back().get().Dump();
if (last_one)    DebugLogger() << "ProductionQueue::Update: 4 last seems imperial project was saved: " << (*last_one).Dump();
full code in the following commit:
https://github.com/agrrr3/freeorion/com ... 5d72b65c04

I'm baffled, can one explain it to me?

Re: C++ question / ProductionQueue::Element suddenly has no

Posted: Tue May 30, 2017 9:29 am
by Geoff the Medio
Should it be "auto& queue_element : dpsim_queue" so that you're taking the address of the Element in the queue rather than of a copy of it?

Re: C++ question / ProductionQueue::Element suddenly has no

Posted: Tue May 30, 2017 1:32 pm
by Ophiuchus
Geoff the Medio wrote:Should it be "auto& queue_element : dpsim_queue" so that you're taking the address of the Element in the queue rather than of a copy of it?
Phew, thanks:!: Don't know how I missed that :)