打印出std :: string

问题描述 投票:2回答:4

在下面的示例代码中

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

我收到以下错误

错误C2679:二进制'<<':没有运算符定义,它采用类型为'class std :: basic_string'的右手操作数

我怀疑有一种特殊的方法可以打印出std::string

请帮我调试一下。

另外,我已经包含了iostream.h,fstream.h和流头文件。

c++ iostream fstream cout
4个回答
3
投票

我怀疑你需要用cout来鉴定std::

    std::cout << result;

或者将using namespace::std添加到cpp文件的顶部。


2
投票

你需要包括<string>


2
投票

cout在<iostream>中定义。让<<语法与std::strings一起使用需要<sstream>

#include <iostream>
#include <sstream>

std::string result = "something";
std::cout << result << " and something else";

0
投票

自从他不活动以来代表@MrLister回答我自己的问题。

我应该包括没有<iostream><fstream>.h。此后using namespace std;应该输入。

例如:

#include <string>
#include <iostream>
#include <fstream>
#include <stdlib>

using namespace std;

非常感谢@MrLister。

感谢@dasblinkenlight。他的回答有所增强。

© www.soinside.com 2019 - 2024. All rights reserved.