Undeclared identifier???

Discussion in 'C++' started by Acken, Dec 26, 2005.

  1. Acken

    Acken Guest

    Hi i have spent almost 2 days on these now..and cannot figure out what
    is wrong?! It seems so simple and innocent , but it is stumbling a
    novice of me. Please help to point out anything wrong and many thanks
    in advance!!!

    "currentVal" below is identified as undeclared identifier in lines
    //??. I thought it was already defined as BYTE (which was typedef
    unsigned char) in another header file.

    static char* g_rgWorksheetFuncs[1][3];
    static char* g_rgStaticWorksheetFuncs[1][3] =
    {" ewma", " BRR"," EWMA};

    for (i = 0; i < 1; ++i)
    for (j = 0; j < 3; ++j)
    BYTE currentVal =
    (BYTE)strlen(g_rgStaticWorksheetFuncs[j]); //ok
    g_rgWorksheetFuncs[j] = new char[currentVal +2]; //??
    memset(g_rgWorksheetFuncs[j], '\0', currentVal +2 ); //??
    memcpy(g_rgWorksheetFuncs[j],
    g_rgStaticWorksheetFuncs[j],currentVal); //??
    g_rgWorksheetFuncs[j]= currentVal; //??
     
    Acken, Dec 26, 2005
    #1
    1. Advertising

  2. Acken

    John Carson Guest

    "Acken" <> wrote in message
    news:
    > Hi i have spent almost 2 days on these now..and cannot figure out what
    > is wrong?! It seems so simple and innocent , but it is stumbling a
    > novice of me. Please help to point out anything wrong and many thanks
    > in advance!!!


    currentVal is out of scope.

    > "currentVal" below is identified as undeclared identifier in lines
    > //??. I thought it was already defined as BYTE (which was typedef
    > unsigned char) in another header file.
    >
    > static char* g_rgWorksheetFuncs[1][3];
    > static char* g_rgStaticWorksheetFuncs[1][3] =
    > {" ewma", " BRR"," EWMA};


    // missing closing " above;

    // you never define i and j below.
    >
    > for (i = 0; i < 1; ++i)
    > for (j = 0; j < 3; ++j)
    > BYTE currentVal =
    > (BYTE)strlen(g_rgStaticWorksheetFuncs[j]); //ok


    The operation of the for() loops ends here, as does the scope of anything
    declared within them.

    > g_rgWorksheetFuncs[j] = new char[currentVal +2]; //??


    // why +2 instead of +1?

    > memset(g_rgWorksheetFuncs[j], '\0', currentVal +2 ); //??
    > memcpy(g_rgWorksheetFuncs[j],


    // why not just use strcpy?

    > g_rgStaticWorksheetFuncs[j],currentVal); //??
    > g_rgWorksheetFuncs[j]= currentVal; //??


    // why are you assigning an unsigned char to a pointer to char?

    Try this:

    static char* g_rgWorksheetFuncs[1][3];
    static char* g_rgStaticWorksheetFuncs[1][3] ={" ewma", " BRR"," EWMA"};

    int main()
    {
    for (int i = 0; i < 1; ++i)
    {
    for (int j = 0; j < 3; ++j)
    {
    BYTE currentVal =
    (BYTE)strlen(g_rgStaticWorksheetFuncs[j]);
    g_rgWorksheetFuncs[j] = new char[currentVal +1];
    memset(g_rgWorksheetFuncs[j], '\0', currentVal +1 );
    memcpy(g_rgWorksheetFuncs[j],
    g_rgStaticWorksheetFuncs[j],currentVal);
    }
    }
    return 0;
    }

    or, better still:

    static char* g_rgWorksheetFuncs[1][3];
    static char* g_rgStaticWorksheetFuncs[1][3] ={" ewma", " BRR"," EWMA"};

    int main()
    {
    for (int i = 0; i < 1; ++i)
    {
    for (int j = 0; j < 3; ++j)
    {
    BYTE currentVal =
    (BYTE)strlen(g_rgStaticWorksheetFuncs[j]);
    g_rgWorksheetFuncs[j] = new char[currentVal +1];
    strcpy(g_rgWorksheetFuncs[j],
    g_rgStaticWorksheetFuncs[j]);
    }
    }
    return 0;
    }

    Even better (for most purposes) would be to use std::string rather than
    char* strings.


    --
    John Carson
     
    John Carson, Dec 26, 2005
    #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. Ron Natalie

    Re: undeclared identifier?

    Ron Natalie, Jul 21, 2003, in forum: C++
    Replies:
    0
    Views:
    433
    Ron Natalie
    Jul 21, 2003
  2. Victor Bazarov

    Re: undeclared identifier?

    Victor Bazarov, Jul 21, 2003, in forum: C++
    Replies:
    2
    Views:
    2,665
    rajiv04
    Jul 22, 2003
  3. John Dibling

    Re: undeclared identifier?

    John Dibling, Jul 21, 2003, in forum: C++
    Replies:
    0
    Views:
    419
    John Dibling
    Jul 21, 2003
  4. Saurabh Aggrawal

    _asm NOP : undeclared identifier

    Saurabh Aggrawal, Dec 10, 2004, in forum: C++
    Replies:
    3
    Views:
    3,607
    Thomas Matthews
    Dec 10, 2004
  5. Replies:
    2
    Views:
    1,812
Loading...

Share This Page