Visual C++ Error 2143

R

Ryan

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.id=readbyte();
idloc[c.id]=i;
hv=readbyte();
c.h=hv>>4; c.v=hv & 0xF;
if (c.h>hmax) hmax=c.h; if (c.v>vmax) vmax=c.v;
c.Tq=readbyte();
}

if (nError) return 0;
return 1;
}

void Frame::extenddata(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]=new int[x+40];
}
}

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

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

void Frame::write() {
write2bytes(0xFFC0); //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.id);
writebyte((c.h<<4)|(c.v));
writebyte(c.Tq);
}
}



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

Victor Bazarov

Ryan said:
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

Drop the parentheses.
the error!
[...]

Victor
 
R

Ryan

Victor,
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
Drop the parentheses.


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
 
P

Phlip

Ryan said:
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?

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.
 
V

Victor Bazarov

Ryan said:
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

I wrote

Ryan wrote then
That didn't seem to work for me.

WTF do you mean by "didn't seem to work"? Did it work or didn't it?
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 *.

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

Yes, 'int*' is a pointer.
If so, I don't
believe you can do this in C++, but rather more of a C operation.
Huh?

Am
I way off track here? If not, I'm still not sure how to fix it. Any
other suggestions?

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
 

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,774
Messages
2,569,599
Members
45,169
Latest member
ArturoOlne
Top