【问题标题】:Error printing std::string using std::string ptr使用 std::string ptr 打印 std::string 时出错
【发布时间】:2018-05-13 04:06:16
【问题描述】:

我正在尝试使用 std::string ptr 打印 std::string ,如下所示,但出现错误:

Error   1   error C2679: binary '<<' : no operator found which takes
 a right-hand operand of type 'std::basic_string<_Elem,_Traits,_Ax>'
 (or there is no acceptable conversion) 

代码:

#include <stdio.h>
#include <iostream>

int main()
{
    std::string * strPtr = new std::string("Hello World!\n");
    std::cout << (std::string)*strPtr;

    delete strPtr;

    return 0;
}

我在这里做错了什么?

【问题讨论】:

  • 您使用的是哪个编译器?因为在 GCC 7.2 中有效。此外,不需要强制转换(std::string)*strPtr。
  • I can't replicate your error。问题中的代码不是导致错误的代码。
  • 我使用的是 Visual Studio 2008
  • 这是 stackoverflow.com/questions/22724064/… 的完整副本,但遗憾的是,如果有人想接手,我已经在其他东西上使用了我的标志。

标签: c++ string stdstring


【解决方案1】:

知道了...我没有包含#include&lt;string&gt;。我的错。添加头文件后,它工作了。

【讨论】:

  • "include #include" 是什么意思?无论如何,由于一个简单的印刷错误而投票结束。
  • 编辑了答案...它的字符串头文件
  • 这个问题可能会帮助像我这样犯愚蠢错误并想知道哪里出了问题的人。
猜你喜欢
  • 1970-01-01
  • 2015-10-22
  • 2011-03-26
  • 1970-01-01
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
相关资源
最近更新 更多