For example, if I have this little function:
string lw(int a, int b) {
return "lw $" + a + "0($" + b + ")\n";
}
….and make the call lw(1,2) in my main function I want it to return "lw $1, 0($2)".
But I keep getting an error: invalid operands of types ‘const char*’ and ‘const char [11]’ to binary ‘operator+’
What am I doing wrong? I pretty much copied an example from class, and changed it to suit my function.
You are trying to concatenate integers to strings, and C++ can’t convert values of such different types. Your best bet is to use
std::ostringstreamto construct the result string:If you have Boost, you can use Boost.Lexical_cast:
And now with C++11 and later there is
std::to_string: