rsk*_*k82 0 c++ operator-overloading
这是我的代码:
#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;
}
Run Code Online (Sandbox Code Playgroud)
结果是:
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:
/.../
Run Code Online (Sandbox Code Playgroud)
应该是test 10.
Jer*_*fin 14
rvalue(10)不能绑定到非const引用.你需要重写你的operator+一个int const &或只是一个int参数.
当你在它时,你想要重写它,所以它也不会修改左操作数.一个operator +=应该修改其左操作数,而是一个operator +不应该.
| 归档时间: |
|
| 查看次数: |
108 次 |
| 最近记录: |