[英]Cannot initialize a variable of type 'char *' with an rvalue of type 'const char *'
Receiving the error Cannot initialize a variable of type 'char *' with an rvalue of type 'const char *'收到错误无法使用“const char *”类型的右值初始化“char *”类型的变量
On this line of the code在这行代码上
char* pos = strrchr (szPathName, '/');
The full code method for it is here;它的完整代码方法在这里;
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;
}
The error occurs because in C++, but not in C, strrchr
is overloaded to return a const char *
if its argument is a const char *
.发生该错误是因为在 C++ 中,而不是在 C 中,
strrchr
被重载以返回const char *
如果其参数是const char *
。 See Why strrchr() returns char*
instead of const char*
?请参阅为什么 strrchr() 返回
char*
而不是const char*
? for a discussion of this.对此进行讨论。
The solution is to follow the pattern in the preceding line, assign const char *
values to variables of the same type.解决方案是按照上一行的模式,将
const char *
值分配给相同类型的变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.