c++ - Stringstream 的 Str 方法将不起作用。 (不同类型的串联)(C++)

标签 c++ string output stringstream

我是 C++ 的新手,我正在尝试启动并运行一个简单的程序。

我在 Windows 系统上使用 Eclipse IDE(C++ 版本)。

我正在尝试连接输出语句,它将组合数字和字符串。

我知道在 Java 中,这是通过 System.out.println() 方法自动完成的。

我能够研究的是在 C++ 中实现它的一种很好的方法是使用字符串流方法。

#include <iostream>
#include <string>
#include "Person.h"

...

string simpleOutput(){
  stringstream ss;

  int a  = 50; // for testing purposes 
  int b = 60;
  string temp = "Random";
  ss << a << b << temp;
  return string output = ss.str();


}

当我尝试编译此代码时得到以下信息:“无法解析方法“str”。

我还没有在任何网页上找到解决方案。 吨 谢谢!

最佳答案

您的主要问题是缺少包含:

#include <sstream>

此外,您的函数中有一个拼写错误,您的 return 语句简直是疯了。通过这些修复,它可以工作:

#include <iostream>
#include <string>
#include <sstream>

string simpleOutput(){
  stringstream ss;

  int a  = 50; // for testing purposes 
  int b = 60;
  string temp = "Random";
  ss << a << b << temp;
  return ss.str();
}

See it live

关于c++ - Stringstream 的 Str 方法将不起作用。 (不同类型的串联)(C++),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21573056/

相关文章:

java - 在 Java 中将一个字符串重新排列为另一个字符串

Java - 从文本文件中读取 double 值

python - 将字符串拆分为所有可能的有序短语

java - 循环、二维数组

c++ - 模板参数类型成员上的运算符 << 仅在 clang 中导致错误

c# - 从 C# 调用非托管 dll。拍2张

c++ - 我可以使用具有超过 1 个参数的赋值运算符构造吗?

c++ - wxFreeChart 值标签图表

c++ - 如何从整个代码的函数中打开文件?

Javascript - 正则表达式 - 全局搜索问题