   extern const confusion...
  Hey all!I''m using Visual C++ .net 2002. When I declare a constant variable in one file, say ''const int aConst = 1;'', then redeclare it in another source file as ''extern const aConst;'', expecting it to be initialised to 1 already, the compiler moans if I try to set the size of the array using this extern const saying that it cannot create a zero sized array. I am forced to explicitly reinitialise aConst to 1 in the new source file.Have I misunderstood something?Thanks all. Beginners and experts will find answers here. 
  arrays declared thus:int a[x];Have their size resolved at compile time.  Therefore x can only be a literal or a preprocessor defined value (which is a literal when the compiler sees it).Your const won''t have a value until run time, so it is of no use to the compiler.Also, in C++ const_cast exists, so the compiler cannot necessarily trust the value of a const to be constant throughout execution. ;  Just put that const in a header and include it in both .cpp.;  Hello Mathematix,I think you problem is this.You declared const int aConst = 1; in one file which in when this file gets compiled then aConst will be assign 1 for this file and at runtime.But in the file you using aConst it is not initialized, since you not reading in the file which has aConst initialized, whence your error of tying to declare an array of zero length, this will happen for all global arrays that will use aConst which are not in the file that has aConst = 1.There are a few solutions for you.One is to put const aConst = 1; in an header file, and hope compile is smart enough only to allocate one occurrence of aConst. There might be compiler switch to make sure theres only one. Or just redeclare extern const int aConst = 1; in the file you want to use aCont in. This is perfectly legal C++ and has to be done in you your case and if only have one occurrence of aConst is a must.An thrid way is to put in a header file and use defines for a C++ file that actual allocate space for aConst.example:in a aConst.h#ifdef _ACONST_CC_EXTERN#elseEXTERN extern#endifEXTERN const int aConst = 1;then in the file that aConst would be defined in put#define _ACONST_CC_#include "aConst.h"#undefine _ACONST_CC_This allows for initailization and only one occurrence of aConst.This should work in M$ NET if it is ANSI compliant.Could also be done as an emun.Lord Bart ;  Two easie ways:1)#define aConst 1since it''s a const int you are not going to change it so you might as well make it a define.2)create your array dynamically:extern const int aConst;int *array; //or any other type. just what you neesarray = new int[aConst]; //or any other type...delete[] array;Make SURE you don''t forget to add the delete[] or you will cause memory leaks.   Sander Maréchal[Lone Wolves Game Development][RoboBlast][Articles][GD Emporium][Webdesign][E-mail]GSACP: GameDev Society Against Crap PostingTo join: Put these lines in your signature and don''t post crap! ;  An even easier way : use a std::vector.std::vector<ElementType> array( aConst );   [ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ][ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ] ;quote:Have their size resolved at compile time. Therefore x can only be a literal or a preprocessor defined value (which is a literal when the compiler sees it).According to MSDN it is possible (and it doesn''t mention M$-specific either).quote:MSDN  In C++, you can use the const keyword instead of the #define preprocessor directive to define constant values. Values defined with const are subject to type checking, and can be used in place of constant expressions. In C++, you can specify the size of an array with a const variable as follows:const int maxarray = 255;char store_char[maxarray];  // Legal in C++; illegal in CIn C, constant values default to external linkage, so they can appear only in source files. In C++, constant values default to internal linkage, which allows them to appear in header files.Did work for me... ;quote:Original post by Fruny  An even easier way : use a std::vector.std::vector array( aConst );   I second that.   [ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ] ;  Ah, nobody listens to me, oh well;quote:Original post by mputters  Ah, nobody listens to me, oh well I second that.   [ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ] 