extern variable and array declare issue c++ -
i have problem extern variable , array declaration it. how declare array global variable located not in declarable file.
file1.cpp
const int size = 10;
mainfile.cpp
extern const int size; void main() { int mas[size]; }
int mas[size];
this line has issue. please guess??
first of constants have internal linkage. these declarations
file1.cpp const int size = 10;
and
mainfile.cpp extern const int size;
refer different entities.
the constant declared in file1.cpp not visible outside corresponding compilation unit.
according c++ standard (3.5 program , linkage)
3 name having namespace scope (3.3.6) has internal linkage if name of
— non-volatile variable explicitly declared const or constexpr , neither explicitly declared extern nor declared have external linkage; or
in mainfile value of size not specified compiler issue error statement
int mas[size];
becuase size of array shall compile-time constant expression.
the simplest solution place constant definition
const int size = 10;
in common headet file included in each translation unit there reference constant.
Comments
Post a Comment