Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/155.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 添加字符串时遇到问题。。。(c+;+;)_C++_String_Operators - Fatal编程技术网

C++ 添加字符串时遇到问题。。。(c+;+;)

C++ 添加字符串时遇到问题。。。(c+;+;),c++,string,operators,C++,String,Operators,由于某种原因,由于我的toString函数,我的源文件将无法编译。它声称无法识别+符号来附加字符串。这是我的密码: string s = "{symbol = " + symbol + ", qty = " + qty + ", price = " + price + "}"; 符号、数量和价格是类中的变量 我从编译器得到以下消息 CLEAN SUCCESSFUL (total time: 55ms) mkdir -p build/Debug/GNU-MacOSX rm -f build/D

由于某种原因,由于我的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)
清洗成功(总时间:55ms)
mkdir-p build/Debug/gnumacosx
rm-f build/Debug/gnumacosx/Stock.o.d
g++-c-g-MMD-MP-mfbuild/Debug/gnumacosx/Stock.o.d-o build/Debug/gnumacosx/Stock.o Stock.cpp
Stock.cpp:在成员函数“std::string Stock::toString()”中:
Stock.cpp:56:错误:在'std::operator+(const std::basic_string&,const_CharT*)[with _CharT=char,_Traits=std::char_Traits,_Alloc=std::allocator]((const char*),qty=“)+((Stock*)this)->Stock::qty'
make:**[build/Debug/gnumacosx/Stock.o]错误1
生成失败(退出值2,总时间:261ms)

有人知道这里发生了什么吗?

您不能调用
std::string::operator+
对int类型,使用


如果
数量
价格
是整数或类似的东西,则可以执行以下操作(在C++11中):


symbol、
qty
price
的类型是什么?symbol-string | qty-int | price-DOUBLE您做了
包含
我假设?是的,我确实包含了,但如果流失败,请确保添加一些错误检查。我使用了所描述的两种方法中的第一种,它似乎有效。谢谢他似乎在OSX上,所以C++11的支持远远不能保证在OS X版本上的支持,并且编译器在OS X上使用C++11的支持非常好。它附带了libc++,这是一个基本上完整的C++11库,包括vs2012库中缺少的东西,而最新版本的clang几乎完全支持C++11语言;我所知道的唯一一个其他人所缺少的特性是继承构造函数(仅在GCC4.8中实现)。
#include <sstream>
#include <string>

std::stringstream ss;
ss << "{symbol = " << symbol << ", qty = " << qty << ", price = " << price << "}";

std::string s = ss.str();
std::string s = "{symbol = " + symbol + ", qty = " + std::to_string(qty) 
                + ", price = " + std::to_string(price) + "}";
string s = "{symbol = " + symbol + ", qty = " + std::to_string(qty) + ", price = " + std::to_string(price) + "}";