Resetting a static int

Discussion in 'C++' started by Allerdyce.John@gmail.com, Mar 14, 2006.

  1. Guest

    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.
     
    , Mar 14, 2006
    #1
    1. Advertising

  2. wrote:
    > 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);
    --
    <mailto:> <http://www.dietmar-kuehl.de/>
    <http://www.eai-systems.com> - Efficient Artificial Intelligence
     
    Dietmar Kuehl, Mar 14, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,251
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,709
    Old Wolf
    Jan 20, 2004
  3. arun
    Replies:
    8
    Views:
    482
    Dave Thompson
    Jul 31, 2006
  4. aling
    Replies:
    8
    Views:
    1,032
    Jim Langston
    Oct 20, 2005
  5. Replies:
    9
    Views:
    465
    James Kanze
    Apr 17, 2007
Loading...

Share This Page