For some reason my source file will not compile because of my toString function. It claims it cannot recognize the + symbol to append strings. Here’s my code:
string s = "{symbol = " + symbol + ", qty = " + qty + ", price = " + price + "}";
symbol, qty, and price are variables in the class
I get the following message from the compiler…
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)
Anyone know what’s going on here?
You can’t call
std::string::operator+on int types, use std::stringstreamOr use std::to_string if you use C++11 and boost::lexical_cast to cast integer types to string first: