V
V
Vladimir Korshunov2021-10-29 00:00:47
C++ / C#
Vladimir Korshunov, 2021-10-29 00:00:47

Why can't I access the unordered_map element by traversal?

I have a class in which one public field is unordered_map from objects of another class, which also has a public field unordered_map already from objects of the third class. I want to iterate over all elements of unordered_map by calling a function from the first class. I made this bypass:

unordered_map<string, interfaceBlock>::const_iterator it, it2;
it = interfaceBlockHash.begin();
        
while (it != interfaceBlockHash.end())
{
    unordered_map<string, button>::const_iterator it_button;
    it_button = it->second.buttonHash.begin();
            
    while (it_button != it->second.buttonHash.end())
    {
            it_button->second.hoverReact(m, w); //проблема
            it_button++;
     }
     it++;
}

On the marked line it gives an error: "'this' argument to member function 'hoverReact' has type 'const button', but function is not marked const". Why is second a CONST button? Why not just a button? That's the whole problem. It also failed to do something through pointers. If I access the same object like this directly from the main:
a.interfaceBlockHash["first"].buttonHash["second"].hoverReact(m, window);

, then everything works fine. What could be the problem? Maybe I'm bypassing these unordered_maps incorrectly or something like that?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2021-10-29
@GashhLab

unordered_map<string, button>::const_iterator it_button;
...
it_button->second.hoverReact(m, w); //проблема

Why is second a CONST button? Why not just a button?

Because const_iterator is the same.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question