Search code examples
c++compiler-errorsdata-members

invalid use of non-static data member c++ student


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.


Solution

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