添加字符串时遇到问题.(C++)
Having trouble with adding strings... (c++)
由于某种原因,由于我的toString函数,我的源文件将无法编译。它声称它无法识别 + 符号来附加字符串。这是我的代码:
string s = "{symbol = " + symbol + ", qty = " + qty + ", price = " + price + "}";
交易品种、数量和价格是类中的变量
我从编译器收到以下消息...
CLEAN SUCCESSFUL (total time: 55ms)
mkdir -p build/Debug/GNU-MacOSX
rm -f build/Debug/GNU-MacOSX/Stock.o.d
g++ -c -g -MMD -MP -MF build/Debug/GNU-MacOSX/Stock.o.d -o build/Debug/GNU-MacOSX/Stock.o Stock.cpp
Stock.cpp: In member function 'std::string Stock::toString()':
Stock.cpp:56: error: no match for 'operator+' in 'std::operator+(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const char*)", qty = ")) + ((Stock*)this)->Stock::qty'
make: *** [build/Debug/GNU-MacOSX/Stock.o] Error 1
BUILD FAILED (exit value 2, total time: 261ms)
有人知道这里发生了什么吗?
不能在 int 类型上调用 std::string::operator+
,请使用 std::stringstream
#include <sstream>
#include <string>
std::stringstream ss;
ss << "{symbol = " << symbol << ", qty = " << qty << ", price = " << price << "}";
std::string s = ss.str();
或者使用 std::to_string 如果使用 C++11 和 boost::lexical_cast 将整数类型首先强制转换为字符串:
std::string s = "{symbol = " + symbol + ", qty = " + std::to_string(qty)
+ ", price = " + std::to_string(price) + "}";
如果qty
和price
是整数或类似的东西,你可以执行以下操作(在C++11中):
string s = "{symbol = " + symbol + ", qty = " + std::to_string(qty) + ", price = " + std::to_string(price) + "}";
例
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 使用mongocxx驱动程序时包含头文件问题