打印出标准::字符串

Printing out std::string

本文关键字:字符串 标准 打印      更新时间:2023-10-16

在下面的示例代码中

    std::string result = exec( "dir" ) ;
    cout<<result;

我收到以下错误

错误 C2679:二进制"<<":未定义采用类型为"类 std::"的右侧操作数的运算符basic_string

我怀疑有一种特殊的方法来打印std::string.

请帮我调试这个。

此外,我还包括iostream.h,fstream.hstream头文件。

我怀疑你需要用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。他的回答提高了一点。