Resetting a static int

A

Allerdyce.John

In my .h, I declare a static int like this:
static int index = 16;
And I calculate another static const like this:
static const int HEADER = (int) pow(2.0, index++);

These 2 lines works and compiles.

However, when I try to reset the value of index, like this:
index = 2;

I get a compile error:
error: expected constructor, destructor, or type conversion before
'=' token

can you please tell me what did I do wrong?

Thank you.
 
D

Dietmar Kuehl

However, when I try to reset the value of index, like this:
index = 2;

Are you doing this within a function or in namespace scope (which
includes the global scope)? The latter is illegal:

static int index = 16; // OK: initialization
index = 2; // ERROR: only declaration are allowed here

int main()
{
index = 2; // OK: expressions are allowed here
}

If you really need to reset the integer at namespace scope (which
I would consider unlikely), you would need to do in the definition
of a namespace object, e.g. like this.

int index_aux = (index = 2);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top