我可以重载现有类中的现有函数/运算符吗?
我正在尝试做:
#include <iostream>
#include <string>
using namespace std;
string& string::operator<<(const string& str) {
this->append(str);
}
但是这给了我错误:
test.cpp:5: error: too few template-parameter-lists
我该怎么做?还是我不能?
除非修改该类的定义,否则不能将成员函数添加到该类。请使用免费功能:
我服从本杰明的回答,在string
对象上创建类似流的接口。但是,您可以改用stringstream
。
使用