I am getting this error
Use of undeclared identifier ‘new’
on this line of code
Byte* decompressedBytes = new Byte[COMPRESSION_BLOCK];
This is my method that this line of code appears in.
// Returns the decompressed version if the zlib compressed input data or nil if there was an error
+ (NSData*) dataByDecompressingData:(NSData*)data{
Byte* bytes = (Byte*)[data bytes];
NSInteger len = [data length];
NSMutableData *decompressedData = [[NSMutableData alloc] initWithCapacity:COMPRESSION_BLOCK];
Byte* decompressedBytes = new Byte[COMPRESSION_BLOCK];
z_stream stream;
int err;
stream.zalloc = (alloc_func)0;
stream.zfree = (free_func)0;
stream.opaque = (voidpf)0;
stream.next_in = bytes;
err = inflateInit(&stream);
CHECK_ERR(err, @"inflateInit");
while (true) {
stream.avail_in = len - stream.total_in;
stream.next_out = decompressedBytes;
stream.avail_out = COMPRESSION_BLOCK;
err = inflate(&stream, Z_NO_FLUSH);
[decompressedData appendBytes:decompressedBytes length:(stream.total_out-[decompressedData length])];
if(err == Z_STREAM_END)
break;
CHECK_ERR(err, @"inflate");
}
err = inflateEnd(&stream);
CHECK_ERR(err, @"inflateEnd");
delete[] decompressedBytes;
return decompressedData;
}
I am not sure why this is appearing like this. This code is from ObjectiveZlib and have read through it several times and am not trying to use it in my own code to decompress a zlib NSData object but am having this stopping me from progressing.
any help would be greatly appreciated.
This code is Objective-C++. You’re trying to compile it as Objective-C. Rename the file to end in
.mminstead of.mand it should work.Specifically, the
newanddeleteoperators are C++ operators. They don’t exist in C. Objective-C is a superset of C, and Objective-C++ is a superset of C++. However since those appear to be the only C++-isms in this code, if you’d rather stick with Objective-C, you can fix it by replacing two lines:new Byte[COMPRESSION_BLOCK]with(Byte*)malloc(sizeof(Byte) * COMPRESSION_BLOCK)delete[] decompressedByteswithfree(decompressedBytes)