1 c++ templates operator-overloading manipulators
我正在尝试重载<< operator for template,我收到了这个错误.
我想要实现的是重载operator <<,它将提供开括号,所有标签项由","和关闭括号分别为'out'.
这是我的代码的一部分:
template <typename T>
class arry{
T *tab;
int n;
public:
arry(T *t, int x) : n(x),tab(t){};
friend std::ostream & operator << (const std::ostream & out, const arry<T> & t)
{
out << "(";
for(int i=0;i<t.n;i++){
out << t.tab[i];
if(i < t.n-1)
out << ", ";
}
out << ")";
return out;
}
};
Run Code Online (Sandbox Code Playgroud)
最糟糕的是,我的构建日志为我提供了230条错误行,此时我有点困惑.
运算符旨在修改流,因此第一个参数不能被const引用.将其更改为
friend std::ostream & operator << (std::ostream & out, const arry& t)
Run Code Online (Sandbox Code Playgroud)