I am having problem with string concatination.
std::deque<std::string> fileNamesVector;
double * res_array;
string *strarr;
size = fileNamesVector.size();
res_array = new double[size];
strarr = new string [size];
I need to append res_array with 4 spaces followed by filenamevector.
How can I do this.
strarr[i] = res_array[i] + " " + fileNamesVector[i];
But it gives errors. Saying “exp must have arithmetic or enum type”
Please help.
In C++, there is no implicit conversion between a double, a
char *orstd::string.res_array[i] + " "is trying to add a double tochar *, so the compiler tries an implicit conversion, but none exists, so it gives you an error saying thatoperator+needs numerical types.You will instead need to explicitly convert
res_array[i]to a string.The example above is from The C++ FAQ, even though there are many stackoverflow questions dedicated to this topic, TC++FAQ deserves the real shout out for being OG 🙂
Or with C++11, use
std::to_string