[英]C++ cannot initialize a variable of type 'char *' with an rvalue of type '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.