从 std::ostream 获取 char*
时间: 2023-09-10 07:04:44 浏览: 65
你可以使用 ostream 类的成员函数 str() 来获取输出流中的字符串。然后使用 c_str() 成员函数将 string 对象转换为 char* 类型的指针。以下是示例代码:
```c++
#include <iostream>
using namespace std;
int main() {
string str = "hello, world!";
cout << str << endl;
char* ptr = (char*) str.c_str();
cout << "char* pointer: " << ptr << endl;
return 0;
}
```
输出结果如下:
```
hello, world!
char* pointer: hello, world!
```
相关问题
[Error] cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&'
这个错误是因为尝试将一个左值绑定到一个右值引用上,具体来说是尝试将一个 std::ostream 对象绑定到一个 std::basic_ostream<char>&& 上。这通常发生在使用移动语义时,例如将一个 std::ostream 对象传递给一个接受右值引用的函数。解决方法是使用 std::move() 将左值转换为右值引用。
[Error] cannot bind ‘std::ostream {aka std::basic_ostream<char>}‘ l value To'std::basic_ostream<char>&&'
This error message means that you are trying to assign a value to an output stream (std::ostream) using an lvalue (an expression that refers to an object that has a memory address) but the type of the lvalue is not compatible with the type of the output stream.
For example, if you have the following code:
```cpp
int main() {
int x = 42;
std::ostream out;
out = x;
return 0;
}
```
You will get the error message because you cannot assign an integer value to an output stream. You need to use the insertion operator (<<) to write the value to the stream:
```cpp
int main() {
int x = 42;
std::ostream out;
out << x;
return 0;
}
```
This will write the value of x to the output stream.
相关推荐
![hpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)