Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extern variable and array declare issue c++

I have a problem with an extern variable and an array declaration with it. How to declare an array with global variable which located not in the 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 an issue. Please any guess??

like image 775
Mikhail Avatar asked May 12 '15 06:05

Mikhail


1 Answers

You can't. An array size must be a constant expression; if it's a variable, then that variable must be const and initialised in the same translation unit, so that its value is available for use as a constant.

If you want to share the value between multiple translation units, define it in a header and include that.

like image 157
Mike Seymour Avatar answered Oct 30 '22 07:10

Mike Seymour