没有运算符“[]”匹配这些操作数 C++

No operator "[ ]" matches these operands C++

const DayOfYearSet::DayOfYear DayOfYearSet::operator[](int index){

        if(index < 0){
            
            cout << "Error! Index cannot be negative.." << endl;
            exit(1);
        }

        if(index >= _size){

            cout << "Error! Index overflows the array size.." << endl;
            exit(1);
        }

        return _sets[index];
    }

    const DayOfYearSet DayOfYearSet::operator+(const DayOfYearSet &other){

        vector <DayOfYear> temp;
        for(auto i = 0; i < other.size(); ++i){
            temp.push_back(other[i]);
            .
            .
            .


        }
    }

嘿,我在代码的 temp.push_back(other[i]) 行遇到问题,编译器说没有运算符“[]”匹配这些操作数。如您所见,我将索引运算符重载为成员函数,但它不起作用?我在这里做错了什么?提前致谢..

编辑:_sets DayOfYear* _sets 作为 class DayOfYearSet.

的私有数据成员

您正在尝试在 const DayOfYearSet &other 上使用 operator[],但该函数被定义为仅适用于 而非 const 的对象.

您应该正确地对这个函数进行 const 限定。

const DayOfYearSet::DayOfYear DayOfYearSet::operator[](int index) const
//                     This function can be used on const objects ^^^^^