Extern variable and array declare C ++ problem

I have a problem with an external variable and array declaration. How to declare an array with a global variable that is not in the declared file.

file1.cpp

const int size = 10; 

mainfile.cpp

 extern const int size; void main() { int mas[size]; } 

 int mas[size]; 

There is a problem in this line. Please guess

+6
source share
4 answers

First of all, constants have an internal relationship. So these ads

 file1.cpp const int size = 10; 

and

 mainfile.cpp extern const int size; 

relate to various objects.

The constant declared in file1.cpp is not visible outside its corresponding compilation unit.

In accordance with the C ++ standard (3.5 Program and communication)

3 A name that has a namespace scope (3.3.6) has an internal relationship if it is a name

- a non-volatile variable that is explicitly declared as const or constexpr , as well as explicitly declared extern or previously declared to have an external connection; or

Size is not specified in mainfile, so the compiler will give an error for the statement

 int mas[size]; 

because the size of the array must be an expression of a compile-time constant.

The easiest solution is to place a permanent definition

 const int size = 10; 

in some general headphone file that will be included in each translation unit, where there is a link to a constant.

+2
source

You can not. The size of the array must be a constant expression; if it is a variable, then this variable must be const and initialized in the same translation unit as its value is available for use as a constant.

If you want to share the value between several units of translation, define it in the header and include it in it.

+6
source

int mas [size];

There is a problem in this line. Please guess

As other users have pointed out, the problem may be that you are trying to create a Variable Lenght Array , which is not allowed in C ++ (but almost come in C ++ 14 as Dynamic Arrays * ).

Some compilers accept VLA as an extension (no standard), so I assume that you are using one that does not have this extension, or its extension is disabled.

Don’t worry, you still have a desktop ...

#define (do not do this)

Assuming the problem is VLA, if we guarantee size as a compile-time value, the problem will be solved like this ...

 // file1.hpp <-- This is now a HEADER not a CPP #define SIZE 10 // mainfile.cpp #include "file1.hpp" void main() { int mas[SIZE]; // accepted, equivalent to int mas[10]. } 

constexpr

C ++ 11 introduced the constexpr * keyword that can be used to achieve your goal

 // file1.hpp <-- This is now a HEADER not a CPP constexpr int size() { return 10; } // mainfile.cpp #include "file1.hpp" void main() { int mas[size()]; } 

transfer

Enumerations are compile-time constants, so you can use them as follows:

 // file1.hpp <-- This is now a HEADER not a CPP enum constant { size = 10 }; // mainfile.cpp #include "file1.hpp" void main() { int mas[constant::size]; } 

* If someone found a better link, please let me know.

+2
source

C ++ does not allow you to specify the size of arrays at runtime. In your example, of course, it is specified during the link, but this does not help the compiler. However, if you use the C ++ 14 compiler, but also for some other compilers (for example, gcc), you can do this, but it is less portable than dynamic memory allocation, and no more convenient than std :: vector < >.

For reference: https://isocpp.org/wiki/faq/freestore-mgmt#dynamic-array-len

+1
source

Source: https://habr.com/ru/post/987019/


All Articles