Visual C++ Error 2143

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan

    Visual C++ Error 2143

    I'm getting a stranger error that I can't seem to figure out. I'm
    using Visual C++ 6.0 and trying to compile the following code: (I've
    pointed out the line that throws the "error C2143: syntax error :
    missing ';' before '['")

    #include <algorithm>
    #include "frame.h"
    #include "myio.h"

    int Frame::read() {
    int Lf; //Length of segment
    int hv;

    Lf=read2bytes() ;
    P=readbyte();
    y=read2bytes(); x=read2bytes();
    nc=readbyte();

    hmax=vmax=-1;
    for (int i=0;i<nc;i++) {
    c[i].id=readbyte();
    idloc[c[i].id]=i;
    hv=readbyte();
    c[i].h=hv>>4; c[i].v=hv & 0xF;
    if (c[i].h>hmax) hmax=c[i].h; if (c[i].v>vmax) vmax=c[i].v;
    c[i].Tq=readbyte();
    }

    if (nError) return 0;
    return 1;
    }

    void Frame::extendda ta(int row) {
    for (;ndatarow<row; ndatarow++) {
    data[ndatarow]=new (int *) [nc]; //<--- This is the line with
    the error!
    for (int i=0;i<nc;i++)
    data[ndatarow][i]=new int[x+40];
    }
    }

    void Frame::cleardat a() {
    for (int i=0;i<ndatarow; i++) {
    for (int j=0;j<nc;j++)
    delete [] data[i][j];
    delete [] data[i];
    }
    ndatarow=0;
    }

    Frame::~Frame() {
    cleardata();
    }

    void Frame::write() {
    write2bytes(0xF FC0); //SOF0 - Start of Frame 0: Baseline DCT
    write2bytes(8+3 *nc); //Length of marker segment
    writebyte(P);
    write2bytes(y); write2bytes(x);
    writebyte(nc);
    for (int i=0;i<nc;i++) {
    writebyte(c[i].id);
    writebyte((c[i].h<<4)|(c[i].v));
    writebyte(c[i].Tq);
    }
    }



    Anyone gots any ideas? I've totally stumped!
    Thanks for your help,
    --Ryan
  • Victor Bazarov

    #2
    Re: Visual C++ Error 2143

    "Ryan" <scheller@stude nt.umass.edu> wrote...[color=blue]
    > I'm getting a stranger error that I can't seem to figure out. I'm
    > using Visual C++ 6.0 and trying to compile the following code: (I've
    > pointed out the line that throws the "error C2143: syntax error :
    > missing ';' before '['")
    > [...]
    > data[ndatarow]=new (int *) [nc]; //<--- This is the line with[/color]

    Drop the parentheses.
    [color=blue]
    > the error!
    > [...][/color]

    Victor


    Comment

    • Ryan

      #3
      Re: Visual C++ Error 2143

      Victor,[color=blue][color=green]
      > > I'm getting a stranger error that I can't seem to figure out. I'm
      > > using Visual C++ 6.0 and trying to compile the following code: (I've
      > > pointed out the line that throws the "error C2143: syntax error :
      > > missing ';' before '['")
      > > [...]
      > > data[ndatarow]=new (int *) [nc]; //<--- This is the line with the error[/color]
      > Drop the parentheses.[/color]


      That didn't seem to work for me. I continued looking at this last
      night and it seems that since nc is an int, that this line is trying
      to cast the value of int nc to int *. Truth be told I'm not sure what
      int * is, but I think it's a pointer. Is this true? If so, I don't
      believe you can do this in C++, but rather more of a C operation. Am
      I way off track here? If not, I'm still not sure how to fix it. Any
      other suggestions?
      Thanks all,
      --Ryan

      Comment

      • Phlip

        #4
        Re: Visual C++ Error 2143

        Ryan wrote:
        [color=blue]
        > That didn't seem to work for me. I continued looking at this last
        > night and it seems that since nc is an int, that this line is trying
        > to cast the value of int nc to int *. Truth be told I'm not sure what
        > int * is, but I think it's a pointer. Is this true? If so, I don't
        > believe you can do this in C++, but rather more of a C operation. Am
        > I way off track here? If not, I'm still not sure how to fix it. Any
        > other suggestions?[/color]

        typedef int * pInt;
        new pInt[nc];

        VC++ has a bug with the "maximum munch" rules (or a similarly named rule).
        The tokens following 'new' must be parsed for as long as the result could be
        a type. But VC++ may have treated the () as argument delimiters.

        VC++ is also supposed to do this properly:

        SimCity & aCity (getSimCity());

        But it won't.

        --
        Phlip



        Comment

        • Victor Bazarov

          #5
          Re: Visual C++ Error 2143

          Ryan wrote:[color=blue][color=green][color=darkred]
          >>>I'm getting a stranger error that I can't seem to figure out. I'm
          >>>using Visual C++ 6.0 and trying to compile the following code: (I've
          >>>pointed out the line that throws the "error C2143: syntax error :
          >>>missing ';' before '['")
          >>>[...]
          >>> data[ndatarow]=new (int *) [nc]; //<--- This is the line with the error[/color]
          >>[/color][/color]

          I wrote
          [color=blue][color=green]
          >>Drop the parentheses.[/color]
          >
          >
          >[/color]

          Ryan wrote then
          [color=blue]
          > That didn't seem to work for me.[/color]

          WTF do you mean by "didn't seem to work"? Did it work or didn't it?
          [color=blue]
          > I continued looking at this last
          > night and it seems that since nc is an int, that this line is trying
          > to cast the value of int nc to int *.[/color]

          No. It is a syntactically incorrect way of invoking "placement new".
          [color=blue]
          > Truth be told I'm not sure what
          > int * is, but I think it's a pointer. Is this true?[/color]

          Yes, 'int*' is a pointer.
          [color=blue]
          > If so, I don't
          > believe you can do this in C++, but rather more of a C operation.[/color]

          Huh?
          [color=blue]
          > Am
          > I way off track here? If not, I'm still not sure how to fix it. Any
          > other suggestions?[/color]

          Since you didn't provide the definition of 'Frame' in your original
          post, I assumed it has a data pointer called 'data', which is an array
          of pointers to int or a pointer to a pointer to int. Is it?

          Now, have you actually _tried_ removing the parentheses? With them the
          statement looks a bit like a "placement new" but in fact is missing some
          vital parts, besides, the expression in the parentheses must evaluate to
          a real pointer. You, OTOH, have a type-id in the parentheses. That's not
          acceptable.

          Write your expression as

          data[ndatarow] = new int*[nc];

          and then read the chapter on dynamic memory allocation in your favourite
          C++ book.

          Victor

          Comment

          Working...