2

I'm a noob who is just starting with programming and I was wondering why do I get an error message which says: invalid use of non-static data member 'Lavirint::n'?

class Lavirint{
private:
    int n, m;
    bool mapa[n + 2][m + 2]; //is this valid?
...
}

Edit - I added a few other variables to the same line, but they don't cause the more errors.

1
  • 2
    What are the values in n and m? If you want to do this, declare mapa as bool **mapa; and then allocate the memory for it in the constructor.
    – scohe001
    Commented Jul 9, 2015 at 17:00

1 Answer 1

4

No. It is not valid. You can't use a member variable in a place where there is no specific object, but also you can't use any value unknown at compile time to size a C array within a class.

The actual error message you quoted refers to that first (and harder to understand) issue. Your member variables only have values within the context of a specific object, but the structure of the class is something in common for all objects and defined before any object is constructed.

The second problem is more fundamental, but the first problem apparently stopped the compiler form noticing the second.

1
  • Thank you so much for the fast reply. The second error I fixed right away, but how can I fix the first one? Commented Jul 9, 2015 at 18:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.