[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'char*' in assignment
时间: 2023-10-16 22:06:35 浏览: 106
浅析string 与char* char[]之间的转换
5星 · 资源好评率100%
This error occurs when you try to assign a `std::string` object to a `char*` variable.
The problem is that `std::string` is a class that represents a string of characters and is not the same as a C-style string (i.e., a `char*` array). Therefore, you cannot assign a `std::string` directly to a `char*` variable.
To fix this error, you can either:
1. Use the `c_str()` method of `std::string` to obtain a pointer to a null-terminated character array that can be assigned to a `char*` variable. For example:
```
std::string str = "hello";
char* cstr = str.c_str();
```
2. Use a `std::vector<char>` instead of a `char*` to store the string. For example:
```
std::string str = "hello";
std::vector<char> vec(str.begin(), str.end());
```
阅读全文