打印出标准::字符串
Printing out std::string
在下面的示例代码中
std::string result = exec( "dir" ) ;
cout<<result;
我收到以下错误
错误 C2679:二进制"<<":未定义采用类型为"类 std::"的右侧操作数的运算符basic_string
我怀疑有一种特殊的方法来打印std::string
.
请帮我调试这个。
此外,我还包括iostream.h,fstream.h和stream头文件。
我怀疑你需要用std::
来限定cout
std::cout << result;
或将using namespace::std
添加到 CPP 文件的顶部。
您需要
包含<string>
cout 在 <iostream>
中定义。要使<<
语法与 std::string
一起使用需要 <sstream>
。
#include <iostream>
#include <sstream>
std::string result = "something";
std::cout << result << " and something else";
代表@MrLister回答我自己的问题,因为他不活跃。
我应该包括<iostream>
和<fstream>
而没有.h
.之后也应该输入using namespace std;
。
前任:
#include <string>
#include <iostream>
#include <fstream>
#include <stdlib>
using namespace std;
非常感谢@MrLister。
多亏了@dasblinkenlight。他的回答提高了一点。
相关文章:
- 如何正确将字符串转换为标准::时间::system_clock::time_point?
- 如何在 C++11 中将标准::字符串转换为标准::u32字符串?
- 从标准::字符串到标准::矢量<bool>的快速转换
- 使用标准库计算字符串中的十进制数
- 如何初始化一个标准::字符串数组?
- C++ - 转换标准::浮点数组的字符串
- 连接和压缩标准::vector<std::字符串的最佳方法>
- 在命名管道中发送标准::字符串流
- 标准::字符串与空字符可能吗?
- 如何从标准::string_view正确创建标准::字符串?
- 从常量字符*、字符*参数到标准::字符串的直接转换接口
- 热将标准::__cxx11::字符串转换为标准::字符串
- 如何将字符*浅复制到标准::字符串?
- 十进制到八进制递归C++.以标准::字符串格式输出
- 对于C字符串,是否有一个标准的C++迭代器
- C++17 编解码器在将标准::字符串转换为标准::字符串时抛出"bad conversion"
- 常量转发引用给出错误 C2440:"正在初始化":无法从"常量标准::字符串"转换为"常量标准::字符串 &&"
- 将来C++的标准库会包含网络、字符串算法,..吗?
- 分段错误标准::矢量<标准::字符串>
- 如何通过标准字符串打印 ostream 变量的值?