I have a problem concatenating a string variable with a float, I have the following:
float c1 = 0, c2 = 0, c3 = 0;
std::string best = "";
Then I use it as follows:
best = "Imagen 1: " + c1;
best = "Imagen 2: " + c2;
best = "Imagen 3: " + c3;
However, it gives me the following errors:
../src/Test.cpp:91:24: error: invalid operands of types ‘const char [11]’ and ‘float’ to binary ‘operator+’
best = "Imagen 1: "+c1;
^
../src/Test.cpp:93:24: error: invalid operands of types ‘const char [11]’ and ‘float’ to binary ‘operator+’
best = "Imagen 2: "+c2;
^
../src/Test.cpp:95:24: error: invalid operands of types ‘const char [11]’ and ‘float’ to binary ‘operator+’
best = "Imagen 3: "+c3;
How could I fix it?
In C++11, you can use the method
to_string
like this:Before C++11, it could be done with
stringstream
:Or better as @Peregring-lk said, you can create a method to do it however you want:
And so for each conversion you want to do. It is best to create your own method: