1

Can someone help me to explain this:

I declare a constant in the headerfile:

const int INCRSIZE; 

Then in the MIL the constant is initialized:

: INCRSIZE(10) 

then later on in a function in the code (its a callback-function used in a gtkmm-GUI

 bool MyWindow::on_drawing_expose_event(GdkEventExpose* event) 

I allocate an array on the stack using this constant

double arrPxStep[INCRSIZE]; 

when I compiling i get the following errors:

-expected constant expression - cannot allocate an array if constant size 0 - arrPxStep unknown size 

I know the constant is initialzed - how come the array cannot use this constant memeber-variable that was alreade initialized in the MIL?

1
  • Member Initialization List Commented Jun 27, 2014 at 10:56

2 Answers 2

2

By the looks of the code you post, INCRSIZE is a non-static member of a class. You can't use this to initialize an array, even if the value is always set to the same in the constructor. It's just the rules.

Make INCRSIZE a static const member, defined within the class, and it will work.

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

Comments

1

It is very hard for compiler to detect, that your variable INCRSIZE is const and initialized. So it processes INCRSIZE as usual integer variable. Since you set value to this variable in constructor of your class, compiler should know something about order of calls of your functions (for you it is obvious, that constructor will be the first called function, but for compiler it is not so easy to analyze it). So, it is not included in Standard of C++ (one of reasons: because it is unnecessary and hard to support).

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.