二进制“[”:未找到接受类型为 'const SortableVector<int>' 的左手操作数的运算符

Binary '[': no operator found which takes a left hand operand of type 'const SortableVector<int>'

所以我 运行 在为我目前所在的 class 编码时遇到了这个问题,我相信代码应该 运行 没问题,但是出现了这个问题:二进制 ' [': 没有找到接受类型 'const SortableVector' 的左手 ope运行d 的运算符 我不太确定如何解决这个问题,有什么建议吗?

我最终查看了 No '==' operator found which takes a left-hand operand of const Type 以查看是否可以在那里找到解决方案,但是我没有,看来我的问题源于我个人没有看到的东西。

#include <iostream>
#include "SortableVector.h"
using namespace std;

int main() {
    const int SIZE = 10;

    SortableVector<int> intTable(SIZE);

    for (int x = 0; x < SIZE; x++) {
        int z;
        cout << "Please enter a number with no decimals: ";
        cin >> z;
        intTable[x] = z;
    }

    cout << "These values are in intTable:\n";
    intTable.print();

    intTable.sortInt(intTable, SIZE);

    cout << "These values in intTable are now sorted: ";
    intTable.print();

    return 0;
}




//SortableVector.h
#include <iostream>
#include <cstdlib>
#include <memory>
#include <vector>
using namespace std;

struct IndexOutOfRangeException {
    const int index;
    IndexOutOfRangeException(int ix) : index(ix) {}
};
template<class T>
class SortableVector {
    unique_ptr<T[]> aptr;
    int vectorSize;
public:
    SortableVector(int);
    SortableVector(const SortableVector &);

    int size() const { return vectorSize; }
    T &operator[](int);
    void sortInt(SortableVector<int>, int);
    void print() const;
};

template<class T>
SortableVector<T>::SortableVector(int s) {
    vectorSize = s;
    aptr = make_unique<T[]>(s);
    for (int count = 0; count < vectorSize; count++) {
        aptr[count] = T();
    }
}

template<class T>
SortableVector<T>::SortableVector(const SortableVector &obj) {
    vectorSize = obj.vectorSize;
    aptr = make_unique<T[]>(obj.vectorSize);
    for (int count = 0; count < vectorSize; count++) {
        aptr[count] = obj[count];
    }
}

template<class T>
T &SortableVector<T>::operator[](int sub) {
    if (sub < 0 || sub >= vectorSize) {
        throw IndexOutOfRangeException(sub);
        return aptr[sub];
    }
}

template<class T>
void SortableVector<T>::sortInt(SortableVector<int> x, int z) {
    int i, j;
    int temp = 0;

    for (i = 0; i < z - 1; i++) {
        for (j = 0; j < z - 1; j++) {
            if (x[j] > x[j + 1]) {
                temp = x[j];
                x[j] = x[j + 1];
                x[j + 1] = temp;
            }
        }
    }
}

template<class T>
void SortableVector<T>::print() const {
    for (int k = 0; k < vectorSize; k++) {
        cout << aptr[k] << " ";
    }
    cout << endl;
}

您的 operator[] returns 对元素的引用,这将允许人们直接更改元素。当您尝试在 const 对象上使用运算符时(当您使用 const 引用将内容传递给函数时),就会出现问题。这将允许某人通过 operator[] 返回的引用更改对象,这会破坏 const-correctness,因此是不允许的。

如果您仍然感到困惑,假设您有一些 class 像这样:

class Foo
{
private:
    int numbers[100];
public:
    int& operator[](const int & pos)
    {
        return numbers[pos];
    }
};

这适用于创建对象和使用括号运算符访问元素。但是,当您尝试创建 const 对象时:

const Foo f;

你可以这样做:

f[3] = 5;

operator[] returns一个引用,可以用来直接改变f中存储的数据。 f 虽然被声明为 const,所以这一定不会发生,编译器会给出错误。

解决方案是有两个版本的 operator[],由它们的 const-ness:

重载
class Foo
{
private:
    int numbers[100];
public:
    int& operator[](const int &pos)
    {
        return const_cast<int&>(static_cast<const Foo&>(*this)[pos]);
    }
    const int& operator[](const int &pos) const
    {
        return numbers[pos];
    }
};

这里non-const版本其实调用了const版本,避免代码重复