C++ 从“const char*”到“char*”的无效转换[-fpermissive]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9200268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:28:09  来源:igfitidea点击:

invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]

c++cstring

提问by freinn

int ::  cadena calculatelenght(const cadena& a, const char* cad)
{
    cadena c;
    int lenght = 0;
    char* punt; punt = cad;
    while(*punt){
        lenght++; punt++;
    }
    return lenght;
}

I have this problem, I want to calculate the length of a C string without using functions like strlen, in other methods of my cadena class I can because is not const char*, but now I don't know what to do.

我有这个问题,我想在不使用类似函数的情况下计算 C 字符串的长度strlen,在我的 cadena 类的其他方法中我可以,因为不是 const char*,但现在我不知道该怎么做。

回答by Kerrek SB

You can declare puntto be of the correct type:

您可以声明punt为正确的类型:

const char * punt = cad;

回答by Joni

You need:

你需要:

const char* punt; punt = cad;