error: assignment of member ‘x::x’ in read-only object

error: assignment of member ‘x::x’ in read-only object

我的代码有一些我无法解决的错误。有错误是:

error: assignment of member ‘Line::sum’ in read-only object sum = tmp;

error: increment of member ‘Line::number’ in read-only object number++;

error: binding reference of type ‘Line&’ to ‘const Line’ discards qualifiers return(*this);

Line & Line :: operator += (const Info & new) const{

    Info * tmp = new Info[number+1];

    for (int p=0; p<number; p++) 
        tmp[p] = sum[p];    // Sin memoria dinámica

    tmp[num_paradas] = new;  // Sin memoria dinámica


    delete [] sum;

    sum = tmp;
    number++;

    return(*this);
}

对于初学者(C++ 17 标准,5.10 标识符)

3 In addition, some identifiers are reserved for use by C++ implementations and shall not be used otherwise; no diagnostic is required.

Select 另一个标识符而不是单词 new

其次,成员函数有限定符const

Line & Line :: operator += (const Info & new) const{
                                              ^^^^^

因此,如果未使用说明符 mutable.

声明相应对象的数据成员,则它们可能不会更改

并且根据 operator += 的逻辑,成员函数应该在没有限定符的情况下声明 const

Line & Line :: operator += (const Info & new) {