c++ - 二进制 '[' : no operator found which takes a left hand operand of type 'const SortableVector<int>'

标签 c++ arrays class templates bubble-sort

所以我在为我当前所在的类(class)编写代码时遇到了这个问题,我相信代码应该可以正常运行但是出现了这个问题:二进制“[”:没有找到采用左手操作数类型的运算符'const SortableVector' 我不太确定如何解决这个问题,有什么建议吗?

我最终查看了 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[] 返回对元素的引用,这将允许人们直接更改元素。当您尝试在 const 对象上使用运算符时(当您使用 const 引用将内容传递给函数时),就会出现问题。这将允许某人通过 operator[] 返回的引用更改对象,这会破坏常量正确性,因此是不允许的。

如果您仍然感到困惑,假设您有这样的类(class):

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

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

const Foo f;

你可以这样做:

f[3] = 5;

operator[] 返回一个引用,可用于直接更改存储在f 中的数据。 f 被声明为 const,所以这一定不会发生,编译器会报错。

解决方案是使用两个版本的 operator[],通过它们的 const-ness 重载:

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

关于c++ - 二进制 '[' : no operator found which takes a left hand operand of type 'const SortableVector<int>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49123648/

相关文章:

c++ - 是否可以对 asio::steady_timer 使用与 asio::serial_port 相同的 asio::io_context?

javascript - 如何比较多个数组中的相同值?

c++ - sh4-linux 上的回溯返回一个函数

javascript - forEach 方法似乎跳过一个条目

c++ - 如何传递二维结构数组给函数?

C++ 指针和类

c++ - 通过 `void *` 和 C++ `extern "C"` 函数调用在 C 中初始化一个 C++ 类

python - 如何为所有调用覆盖导入的 python 类

android - 如何用C++编写Android服务

c++ - 将 `nullptr` 分配给 `bool` 类型。哪个编译器是正确的?