-1

I have this simple code and I am trying to make it so that the object uses the array and goes through it but I get error in the bj.mas[20] = {1, 1, 1, 1, 1}; statement: "cannot convert '' to 'int' in assignment". And in the for statement I get: expected primary-expression befor ']' token. What is causing the problem and how to fix it?

class C{
    public:
        int mas[20] = {};
};
int main(int argc, char** argv) {
    C obj;
     obj.mas[20] = {1, 1, 1, 1, 1};
    
    for(int i : obj.mas[]){
        cout << i << "\n";
    }
    
    return 0;
}
dbz
  • 21
  • 5

1 Answers1

3

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...

Aconcagua
  • 24,880
  • 4
  • 34
  • 59
  • This is a good explanation. I tried your solution and for filling the array works, but when trying get the element from the array ( `for(int i : obj.mas)` ) it doesn't. – dbz Nov 03 '21 at 12:36
  • Cannot [reproduce](http://coliru.stacked-crooked.com/a/ecbb89b9b98d20ea) – I assume there's another error you didn't include here. Or did you possibly compile with a standard older than C++11 (e.g. GCC before 6.1 has C++ 98 as default, so you need to specify `-std=c++11` or more recent, as far as supported – but then `std::array` should be lacking as well)? – Aconcagua Nov 03 '21 at 12:56
  • this is my entire code and I just downloaded dev-C++ and started learning c++ – dbz Nov 03 '21 at 16:09
  • @dbz You might want to print your compiler's version (e.g. `GCC --version`) and additionally provide the command line you compile your code with – as mentioned, might be related to standard version you compile with. – Aconcagua Nov 03 '21 at 16:41
  • this is what I am getting `gcc (MinGW.org GCC-6.3.0-1) 6.3.0` from cmd, but I have installed minGW for VS code and dev-c++, so I dont know witch of them it is showing – dbz Nov 03 '21 at 20:11
  • `Dev-C++ 5.0 (4.9.9.2) with Mingw/GCC 3.4.2` is what is written on web site, so it seems to be bundled with outdated GCC version. You might consider switching to [embarcadero version](https://www.embarcadero.com/ru/free-tools/dev-cpp) having integrated more recent GCC (9.2), or you find out how to exchange GCC in your dev-c++ (which seems to be possible, you'll have to look into the settings). If we are at the topic already: You might, too, consider switching to MinGW64, yet another fork, allowing to compile 64-bit applications. – Aconcagua Nov 04 '21 at 09:48