包括在 <sstream> Xcode 9.2 中不起作用
include <sstream> not working in Xcode 9.2
我现在正在学习 c++,并且在尝试连接字符串和数字时遇到了问题sstream
因为它对我不起作用。
我得到的错误是:
二进制表达式的操作数无效("basic_string,标准::__1::分配器>"和"int"(。
代码如下:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
string name = "Jane";
int age = 28;
string info = "Name: " + name + "; age:" + age;
cout >> info >> endl;
return 0;
}
您的问题不在于sstream
,而是与此处的这一行有关:
string info = "Name: " + name + "; age:" + age;
一切看起来都很好,直到"; age:" + age
.不能添加string
和int
。您可以使用std::to_string
将int
转换为string
:
string info = "Name: " + name + "; age:" + to_string(age);
to_string
是在 C++11 中添加的,但如果由于某种原因你不能使用 C++11,你可以使用字符串流自己定义这个函数:
template <typename T>
string to_string(const T &thing) {
std::ostringstream oss;
oss << thing;
return oss.str();
}
另请注意,您在cout
上使用提取运算符。你可能的意思是:
cout << info << endl;
要使用字符串流创建一个连接字符串和其他类型的新字符串,您需要如下所示的内容:
int meaning = 42;
ostringstream os;
os << "The meaning of life is " << meaning;
string s = os.str();
相关文章:
- 我的神经网络不起作用 [XOR 问题]
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- C++为什么尽管我调用了void函数,它却不起作用
- 为什么在保护模式下继承升级不起作用
- 循环在计数器中不起作用
- 在其他文件中创建类时在 c++ 项目中不起作用
- Visual studio代码重构似乎不起作用(例如,重命名符号-f2)
- 为什么二进制搜索在我的测试中不起作用
- 我的代码中有错误吗?使用BGI图形的C++代码对我不起作用
- 为什么 const std::p air<K,V>& 在 std::map 上基于范围的 for 循环不起作用?
- 包括在 <sstream> Xcode 9.2 中不起作用
- 找不到WCHAR.H文件-Xcode 9.0中断QT创建者 - 旧修复程序不起作用
- XCode更新后,标头文件在CLION中不起作用
- Xcode 5 带有 %s 格式说明符的 LLVM printf 在调试控制台中不起作用
- Xcode 5 c++ fstream(ifstream 和 ofstream)不起作用
- std::wofstream::open 在 MAC/Xcode 上不起作用
- Xcode 代码感知不起作用(找不到符号)
- 在模板类中声明模板成员变量在 Xcode 中不起作用
- Enable_shared_from_this在xcode 5上不起作用