简体   繁体   中英

C++ error:Assignment of read-only location

Please help me to find the problem, I am totally lost why I can't modify my list. As you can see I did not declared anything as const..

Here is the code:

struct Carte 
{   
    Carte(initializer_list<initializer_list<int>>  iInp){}
    initializer_list<initializer_list<int>> aVect;
};


.....
void changeValue(Carte& iCarte, int iValA, int iValB)
{   
    initializer_list<initializer_list<int>> carte = iCarte.aVect;
    initializer_list<initializer_list<int> >::iterator carte_iterator;
    initializer_list<int>::iterator carteRow_iterator;

    //changing the value
    int aLocalI = 0;
    int aLocalJ = 0;

    for(carte_iterator = carte.begin();carte_iterator!=carte.end();++carte_iterator) 
    {
        if (aLocalI == iValA)
        {
            for(carteRow_iterator = (*carte_iterator).begin();carteRow_iterator!=(*carte_iterator).end();++carteRow_iterator) 
            {
                if (aLocalJ == iValB)
                {                   
                    *carteRow_iterator = 1; // ERROR HERE
                }
                aLocalJ++;
            }
        }
        aLocalI++;
    }
}

Many thanks in advance, Julia

From http://en.cppreference.com/w/cpp/utility/initializer_list :

An object of type std::initializer_list<T> is a lightweight proxy object that provides access to an array of objects of type const T .

The elements of an initializer_list are always const , and thus *carteRow_iterator is const .

If you want a modifiable list of objects, use std::vector or std::array .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM