[Error] cannot convert 'std::string {aka std::basic_string<char>}' to 'char*' in assignment
时间: 2023-10-16 19:06:35 浏览: 52
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());
```
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)