Expression must be modifiable lvalue

时间:2016-04-04 17:00:12

标签: c++ visual-c++

I'm trying to store a structure of capacity 6 in another structure.

struct eachElement {
    float centerX;
    float centerY;
    int flagMountain;
};


eachElement cn[6];


struct characters {
     eachElement each[6];
};


characters chars[1500];


float strtPt = 235.0;
float initializer = strtPt;
float endPt = 120.0;
float holder = endPt;
int count = 0;
int ctr = 0;
int cr = 0;
int countCharacters = 0;
int dup = 0;


while (holder < m_img_height) {

    for (float i = initializer ; i < m_img_width - 500; ) {  

        float j = holder;  

        int ck = 1;

        while (ck < 4) {

            cvCircle(image, cvPoint(i, j), 3, cvScalar(0, 255, 0), 1);
            cr = ctr++;
            cn[cr].centerX = i;
            cn[cr].centerY = j;
            cn[cr].flagMountain = 1;




            cvCircle(image, cvPoint(i + 5, j + 5), 1, cvScalar(0, 255, 0), 1);


            j += 9.448;

            count++;


            ck++;


        }




        if (count == 6) {
            i += 23.811;

            count = 0;
            ctr = 0;

            dup = countCharacters++;

             chars[dup].each = cn;

        }
        else
            i += 9.448;
}

    holder += 56.686;

}

In this line,

chars[dup].each = cn;

it gives me an error saying expression must be a modifiable lvalue.

Even though I'm assigning it to the same type, I got this error.

Any help would be appreciated.

3 个答案:

答案 0 :(得分:2)

I don't know what you try to achieve with your code, but you must specify an array index for the each member array to access any of it's innards:

chars[dup].each[0] = cn[0];
            // ^^^     ^^^

The array start address cannot be changed

chars[dup].each = cn;

hence the compiler error.


To fix that use std::copy():

std::copy(std::begin(cn),std::end(cn),std::begin(chars[dup].each));

答案 1 :(得分:1)

Arrays do not have the copy assignment operator. You have to copy each element of one array into another array.

Thus this statement

chars[dup].each = cn;

is wrong.

You can use standard algorithm std::copy to copy one array into another. For example

#include <algorithm>

//...

std::copy( std::begin( cn ), std::end( cn ), std::begin( chars[dup].each ) );

Or if the compiler does not support function std::begin and std::end then you can just write

#include <algorithm>

//...

std::copy( cn, cn + 6, chars[dup].each );

or

std::copy( cn, cn + sizeof( cn ) / sizeof( *cn ), chars[dup].each );

答案 2 :(得分:0)

Your struct has fixed buffer

struct characters {
     eachElement each[6];
};

and your assignemt

chars[dup].each = cn;

will be legal (I don't know if will make sense) if the struct were like this:

struct characters {
         eachElement * each;
    };

The code is hard to understand for me, I cannot propose an equivalent solution for your goal. But such is the background of error message

maybe, given your declaration - but I don't understand the goal - this style would work:

for(int i=1;i<6;i++)
    chars[dup].each[i] = cn;