1

我正在尝试为模板重载 << 运算符,但出现此错误。

我想要实现的是重载运算符<<,它将提供左括号,所有标签项由“,”分隔,右括号为“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;
    }
};

最糟糕的是,我的构建日志为我提供了 230 条错误行,此时我有点困惑。

4

1 回答 1

6

操作符是用来修改流的,所以第一个参数不能被const引用。将其更改为

friend std::ostream & operator << (std::ostream & out, const arry& t)
于 2015-05-19T20:32:19.743 回答