ISO C++11 does not allow conversion from string literal to 'char *'
时间: 2024-04-24 10:25:18 浏览: 21
In ISO C++11, string literals are considered to have type `const char[]`, which means they cannot be converted to a non-const `char*` directly. This change was made to enforce stricter type safety and prevent accidental modification of string literals.
If you need to pass a string literal to a function that expects a `char*` argument, you can use a const-correct approach by declaring the parameter as `const char*`. For example:
```cpp
void myFunction(const char* str) {
// Function implementation
}
int main() {
myFunction("Hello, world!"); // Passing string literal as const char*
return 0;
}
```
Alternatively, if you have control over the function signature, you can update it to accept a `const char*` parameter instead of `char*` if the function doesn't need to modify the string.