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