-3
class Pelicula
{
private:
    int cantActores=10;
    Actor listaActores[cantActores];
public:
    void setlistaActores(int f){cantActores=f;};
    int getlistActores(){return cantActores;};
}

It does keeps me saying invalid non static member

  • 3
    please do not paraphrase the error message, instead include it verbatim in the question – 463035818_is_not_an_ai Apr 12 '20 at 21:41
  • Please provide a [example]. That means at least a simple `main()` function that duplicates the error you're seeing. Make sure it actually compiles, and make sure it duplicates the error. Then tell us the EXACT text of the error, and indicate what line it occurs if you know. – JohnFilleau Apr 12 '20 at 21:41
  • 2
    `Actor listaActores[cantActores];` -- This is not valid C++. – PaulMcKenzie Apr 12 '20 at 21:42
  • 1
    @JohnFileau dont get me wrong, I am among the biggest fans of mcve, but this code has already all that is needed, perhaps a declration of `Actor` would be good that even that isnt really relevant – 463035818_is_not_an_ai Apr 12 '20 at 21:42
  • 2
    Yes there is, what you can't have are VLAs in C++, for that you can use `std::vector`. – anastaciu Apr 12 '20 at 21:43
  • 1
    Does this answer your question? https://stackoverflow.com/questions/1887097/why-arent-variable-length-arrays-part-of-the-c-standard/ – 463035818_is_not_an_ai Apr 12 '20 at 21:45
  • @idclev I find that the mcve is subjective. Some people need more information. Some people need less. This was one that wasn't obvious to me. – JohnFilleau Apr 12 '20 at 21:50
  • @JohnFileau not quite subjective really. It should reproduce the error and no others, thats the most important, missing `;` and declaration of `Actor` gets the job done, no need for `main`: https://wandbox.org/permlink/BPJW9SYdJATEqYiT – 463035818_is_not_an_ai Apr 12 '20 at 21:53
  • @JohnFileau though yes, sometimes it isnt obvious what isnt needed. I also rather ask once too much for a mcve, rather than not ;) – 463035818_is_not_an_ai Apr 12 '20 at 21:57
  • I find the lack of text describing the problem more obstructive than a lack of code. With better text (including the exact error message and an indication of which line triggers it), people might be more accepting of this particular code snippet as "complete enough". – JaMiT Apr 12 '20 at 22:27

1 Answers1

1

You may not use a non-static non-constant data member as a size of a data member of an array type.

Moreover variable length arrays is not a standard C++ feature.

What you need is to declare a template class like for example

template <size_t cantActores>
class Pelicula
{
private:
    Actor listaActores[cantActores];
public:
    size_t getlistActores() const {return cantActores;};
};

So if you need objects of the class that would contain arrays of different sizes then just specify the size as template argument.

Another approach is to use the standard class template std::vector instead of the array.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335