【发布时间】:2020-05-08 10:41:08
【问题描述】:
我认为这是后来的 Xcode 问题(我使用的是 Xcode 11),因为此代码以前在旧版本中很好。
在我的main.mm 文件中,我有以下内容;
int main(int argc, char *argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString * path = [[NSBundle mainBundle] pathForResource: @"fd" ofType: @"dat"];
NSData* image = [NSData dataWithContentsOfFile:path];
const char* szPathName = [path UTF8String];
const char* pos = strrchr (szPathName, '/');
//char* pos = strrchr (szPathName, '/');
*pos = 0;
if (CreateFaceFatted(szPathName) == 0)
{
NSLog(@"Init Dictionary failed");
}
int retVal = UIApplicationMain(argc, argv, nil, nil);
[pool release];
return retVal;
}
我已将char* pos = strrchr (szPathName, '/'); 更改为const char* pos = strrchr (szPathName, '/');,否则它会抛出一个
Cannot initialize a variable of type 'char *' with an rvalue of type 'const char *'
所以通过改用 const char,我至少可以继续进行,但是在以下部分;
*pos = 0;
我收到一个错误Read only variable is not assignable
我对与 obj-c 混合的 c++ 代码有一点了解,但我正在努力想办法让它编译
【问题讨论】:
-
C++ 标准库中有两个
std::strrchr重载:一个采用const char*第一个参数并返回一个const char*结果,另一个采用char *参数并返回一个char *结果。由于szPathName的常量性,您使用的是前者。要使用后者,szPathName需要是非常量的。
标签: c++ ios objective-c xcode