我收到此错误
使用未声明的标识符'new'
在这行代码上
Byte* decompressedBytes = new Byte[COMPRESSION_BLOCK];
这是我的方法,此行代码将出现在其中。
// 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;
}
我不确定为什么会这样出现。这段代码来自ObjectiveZlib,已经阅读了好几次,没有尝试在我自己的代码中使用它来解压缩zlib NSData对象,但是却阻止了我的前进。
任何帮助将不胜感激。
最佳答案
这段代码是Objective-C++。您正在尝试将其编译为Objective-C。重命名该文件以.mm
而不是.m
结尾,它应该可以工作。
具体来说,new
和delete
运算符是C++运算符。它们在C中不存在。Objective-C是C的超集,而Objective-C++是C++的超集。但是,由于这些似乎是该代码中唯一的C++形式,因此,如果您愿意使用Objective-C,则可以通过替换两行来解决它:
new Byte[COMPRESSION_BLOCK]
替换(Byte*)malloc(sizeof(Byte) * COMPRESSION_BLOCK)
delete[] decompressedBytes
替换free(decompressedBytes)
关于ios - Objective-C警告,使用未声明的标识符“new”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13852215/