This is my code:
#include <iostream>
#include <string.h>
using namespace std;
string& operator+(string & lhs, int & rhs) {
char temp[255];
itoa(rhs,temp,10);
return lhs += temp;
}
int main() {
string text = "test ";
string result = text + 10;
}
The result is:
test.cpp: In function 'int main()':
test.cpp:15:26: error: no match for 'operator+' in 'text + 10'
test.cpp:15:26: note: candidates are:
/.../
And should be test 10.
An rvalue (
10) can’t bind to a non-const reference. You need to rewrite youroperator+to take either anint const &or just anintas its parameter.While you’re at it, you want to rewrite it so it doesn’t modify the left operand either. An
operator +=should modify its left operand, but anoperator +should not.