1

Hallo I am trying to declare an array in a struct and when I declare the size as a constant variable it shows me this error

a nonstatic member reference must be relative to a specific object

and if I declare the constant variable out of the struct scope the error goes away.

here is my code:

  struct Student{

      string name;
      string birthday;
      int studyYear;
      string Faculty;
      string Department;
      const int MaxNrOfCrs = 10;  // here is my error 
      const int Grade = 1;        // The same error appears here also
      Course crs[MaxNrOfCrs][Grade];
      bool payment;
  };

but when I try to take the two constants out of the struct Student scope the error does not appear any more

  const int MaxNrOfCrs = 10;  // The error vanishes here
  const int Grade = 1;        // The same error vanishes here also

  struct Student{

      string name;
      string birthday;
      int studyYear;
      string Faculty;
      string Department;
      Course crs[MaxNrOfCrs][Grade];
      bool payment;
  };
3
  • @JesperJuhl GCC 9.1 for example: wandbox.org/permlink/gB8jhjXgRI2HsbQF Nov 23, 2019 at 16:46
  • no i dont think it is valid because he is trying to use those variables inside the same struct to initalzie the array before its being constructed
    – Spinkoo
    Nov 23, 2019 at 16:48
  • @JesperJuhl on the provided link, he is using this flags $ g++ prog.cc -Wall -Wextra -I/opt/wandbox/boost-1.70.0/gcc-9.1.0/include -std=c++17 So it's c++17.
    – borievka
    Nov 23, 2019 at 16:51

1 Answer 1

3

Even though a class member is declared const with a default value, it can still be constructed in each individual instance of the class with some different value, via a constructor or, as in this case, aggregate initialization, perhaps.

Temporarily removing the invalid array declaration, here's some valid C++:

#include <string>
using namespace std;

struct Student{

      string name;
      string birthday;
      int studyYear;
      string Faculty;
      string Department;
      const int MaxNrOfCrs = 10;  // here is my error 
      const int Grade = 1;        // The same error appears here also
      bool payment;
};

Student s{"John", "1/1/2000", 2017, "Engineering", "Mechanical",
    20, 5, true};

So, this ends up constructing a class instance with MaxNrOfCrs containing 20.

So, what do you propose the size of your class array member to be here? It can't be different for every instance of the class, obviously.

Here, a const only means that this class member is constant after an instance of this class is constructed. And it can be constructed with any value, therefore you can't really use this class member to specify the immutable size of an array (there are a few other reasons too).

In the other case, you declared a global const int. End of story. Nothing could possibly change that. Hence you can use it to specify the size of some array.

2
  • Thanks alot for your answer it was very helpful, so what I understood from your answer is: variables of struct can have many values for each object constructed after decleration and if I make any variable const that means each constructed object from that struct should have the same value for the same variable and that is not allowed here. right? and at the other hand you can however still changeing the const variable with each new object construction by initialization. Regarding the array decleration it is a struct called Course which I declared before this student struct Nov 23, 2019 at 17:49
  • 1
    There are several different things here: how const class members work; default values of class members; and the fact that, in C++, arrays must have fixed constant size. A const class member does not mean that all instances of the class have the same value for this class member, it's default initialization value. This is because it can be explicitly initialized to some other, non-default, when a class instance is constructed. That's why it cannot be used to define an array's size. Nov 23, 2019 at 23:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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