3

I want to declare the length of an array member variable using a constant static variable of the class. If I do:

// A.h
#include <array>
using namespace std;
class A {
      array<int,LENGTH> internalArray;
public:
      const static int LENGTH;
};

// A.cpp
#include "A.h"
constexpr int A::LENGTH{10};

There is the error in A.h: "'LENGTH' was not declared in this scope", when declaring internalArray.

I find it weird because how come a class member variable, i.e. LENGTH, is out of scope inside the class? The only workaround I found was to move the initialization from A.cpp to A.h:

// A.h
#include <array>
using namespace std;
constexpr int LENGTH{10};
class A {
      array<int,LENGTH> internalArray;
public:
      const static int LENGTH;
};

But as I understand, first these are two different variables: the global namespace scope LENGTH and the class scope LENGTH. Also, declaring a variable in .h (outside class A) will create an independent LENGTH object in every translation unit where the header is included.

Is there a way to specify the length of the array with a static class-scoped variable?

5
  • how come a class member variable <...> is out of scope inside the class Because the compiler reads the files top-down. If some name is undeclared at the point it sees it - error is raised. And, clearly, LENGTH is below first usage of it. Commented Jun 13, 2017 at 20:20
  • 2
    You probably need the value of LENGTH defined in the header (and above the definition of the array); without it, the size of an instance of A is unavailable to anyone except A.cpp, which makes it impossible for anyone else to instantiate your class (how do they know how much memory to reserve for an instance otherwise?). Have you tried defining it per the guidelines here? That's for C-style arrays, but it's the same idea; you need to have a defined value for LENGTH to use it as a compile-time constant. Commented Jun 13, 2017 at 20:20
  • @Algirdas Preidžius you are right I had to put the declaration of LENGTH first. Commented Jun 13, 2017 at 20:44
  • @Vlad from Moscow you were right I had to put the declaration of LENGTH first. Commented Jun 13, 2017 at 20:44
  • @ShadowRanger thanks for the info, I have to finish coding something and I'll take a look at your link. Commented Jun 13, 2017 at 20:45

1 Answer 1

4

Try this:

#include <array>

class A {
 public:
  static const size_t LENGTH = 12;
 private:
  std::array<int,LENGTH> internalArray;
};

int main(){
  A a;
}

You can declare the value of LENGTH right in your class header, no need to have it be a separate global variable or for it to live in the cpp file.

Use the size_t type, since that is what the std::array template expects.

If that public/private arrangement is bad for you, know that you can include multiple public/private indicators in the class header.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot Richard, this does the trick. I can modify the public/private arrangement without problem, btw. Thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.