简体   繁体   English

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

[英]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.

相关问题 C ++无法使用'char'类型的右值初始化'char *'类型的变量 - C++ cannot initialize a variable of type 'char *' with an rvalue of type 'char' 无法使用类型为'value_type *'的rvalue初始化类型为“const unsigned char *”的参数(也称为char *) - Cannot initialize a parameter of type “const unsigned char *” with an rvalue of type 'value_type *' (aka char*) 值类型const char不能用于初始化char *类型的实体 - Value type const char cannot be used to initialize an entity of type char* 无法使用“const char *”类型的左值初始化“char *”类型的成员子对象 - Cannot initialize a member subobject of type 'char *' with an lvalue of type 'const char *' “const char*”类型的值不能用于初始化“char *”类型的实体 - A value of type "const char*" cannot be used to initialize an entity of type "char *" 无法使用“const Node *”类型的右值初始化“Node *”类型的变量 - cannot initialize a variable of type 'Node *' with an rvalue of type 'const Node *' 无法使用类型为“const char [X]”的左值初始化“const signed char *”类型的成员子对象 - Cannot initialize a member subobject of type 'const signed char *' with an lvalue of type 'const char [X]' 错误:无法初始化返回 object 类型为“const char”,左值类型为“const char [34]” - error: cannot initialize return object of type 'const char' with an lvalue of type 'const char [34]' 错误:无法使用“const int *”类型的右值初始化“int *const”类型的变量 - error: cannot initialize a variable of type 'int *const' with an rvalue of type 'const int * 从'const char *'类型的右值初始化'const char *&'类型的非const引用的无效初始化 - invalid initialization of non-const reference of type 'const char*&' from an rvalue of type 'const char *'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM