1

I'm a beginner C++ programmer and not sure why this won't work:

#include <iostream>
using namespace std;

class Hello
{   private:
        int mess[];
    public:
        Hello() {
            mess = { 1, 3, 4, 546, 2 };
        }
};

int main()
{
    Hello h;
    return 0;
}

Keeps saying: error: assigning to an array from an initializer list

it's unhappy with the way I initialised the array "mess = { 1, 3, 4, 546, 2 };"

Why is this happening and how do I fix it?

Thank you!

Mayron
  • 2,146
  • 4
  • 25
  • 51

1 Answers1

3

C-array are not assignable (and you don't give it a size)

You may initialize it in constructor initializer:

class Hello
{   private:
        int mess[5];
    public:
        Hello() : mess{ 1, 3, 4, 546, 2 } {}
};
Jarod42
  • 203,559
  • 14
  • 181
  • 302