Well, at first obj.mas[20]
does not address the array as a whole, but only one single value of, the one at index 20, so trying to assign an array to of course needs to fail.
However, as this array has a length of 20, 20 is not a valid index, so you are trying to write beyond array bounds, which is undefined behaviour. Anything might happen, if you're unlucky (or rather lucky? – at least you notice the error immediately instead of something going wrong at a far later point), your programme even crashes.
If you want to address the array as a whole, you do so by its name only: obj.mas = ...
– however assigning arrays as a whole simply is not supported in C++, so all you can do instead is assigning the values individually one by another:
for(size_t i = 0; i < 5; ++i)
{
obj.mas[i] = 1;
}
Similarly the index operator applied to the array in the range-based for loop is wrong; at first there's the argument (index) missing, and if it was added again it would address a single int
within the array – which you cannot iterate over. So the loop must look like:
for(int i : obj.mas)
//or alternatively:
for(auto i : obj.mas) // remains compatible, if you change underlying type
Apart from all these problems: Raw arrays are a legacy concept, they come with quite a number of issues (like decaying to pointers automatically and loosing size information that way) – for which reason std::array
has been introduced, avoiding these issues (you'd pass them to functions by reference – though these usually being templated, as array size is a compile time constant as well):
std::array<int, 20> mas;
std::array
can accept such an initialiser list (constructing a new std::array
which is then assigned), apart from the issues above remain, though.
Finally: A good C++ book should have explained the issues to you as well...