繁体   English   中英

无法使用“const char *”类型的右值初始化“char *”类型的变量

[英]Cannot initialize a variable of type 'char *' with an rvalue of type 'const char *'

收到错误无法使用“const char *”类型的右值初始化“char *”类型的变量

在这行代码上

char* pos = strrchr (szPathName, '/');

它的完整代码方法在这里;

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];
    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;
}

发生该错误是因为在 C++ 中,而不是在 C 中, strrchr被重载以返回const char *如果其参数是const char * 请参阅为什么 strrchr() 返回char*而不是const char* 对此进行讨论。

解决方案是按照上一行的模式,将const char *值分配给相同类型的变量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM