二进制'[':没有找到哪个运算符采用'const SortableVector类型的左手操作数 “

问题描述 投票:0回答:1

因此我遇到了这个问题,因为我正在为我目前所在的类编写代码,我相信代码应该运行良好但是这出现了:二进制'[':没有找到运算符,它采用类型'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;
}
c++ arrays class templates bubble-sort
1个回答
1
投票

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

如果你感到困惑,那就说你有这样的课:

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-perss重载:

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];
    }
};

这里,非const版本实际上调用const版本以避免代码重复。

© www.soinside.com 2019 - 2024. All rights reserved.